Hello there,
I’m new to hibernate and using it with MySQL.
I have two entities mapped together.
@Entity
public class Game extends AbstractDefaultEntity implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@JsonManagedReference
@NotNull
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "seasonid")
private Season season;
@NotEmpty(message = "Please enter title")
private String title;
@NotNull
private Date date;
private Time time;
private Integer maxOrder;
@JsonManagedReference
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "gameid")
private Set<Ticket> tickets;
@Entity
public class Ticket extends AbstractDefaultEntity implements java.io.Serializable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@NotNull
@JsonManagedReference
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "gameid")
@NaturalId
private Game game;
@NotEmpty(message = "Please enter title")
private String title;
private Float price;
private Integer maxorder;
private String type;
Each game can have 0-n tickets.
When i add a ticket and select the game, the reference is build up correctly.
{
"id": 1,
"season": {
"id": 1,
"title": "Saison 2021/2022",
"active": "true"
},
"title": "FC Bayern-BVB",
"date": "2020-04-20",
"time": "17:00",
"maxOrder": 1,
"tickets": [
{
"id": 1,
"title": "Stehplatz München",
"price": 20.0,
"maxorder": 1,
"type": "sitting",
"displayTitle": "Stehplatz München"
},
{
"id": 2,
"title": "Sitzplatz München",
"price": 50.0,
"maxorder": 1,
"type": "sitting",
"displayTitle": "Sitzplatz München"
}
]
}
But now I want to know, which tickets belong to Gameid=1.
I’m using the CriteriaBuilder and get the error “Parameter value [1] did not match expected type”
I can not use static querys as I have to filter the fields dynamically given by the UI.
The incoming parameters can change, so I have to select by title one time or by gameid the other time
public static Predicate getListFilter(UriInfo uriInfo, Root root, CriteriaBuilder cb, CriteriaQuery query) {
List<Predicate> predicateList = new ArrayList<>();
if (!uriInfo.getQueryParameters().containsKey("filter")) {
System.out.println("NO Filter found");
return null;
}
String filter = uriInfo.getQueryParameters().getFirst("filter");
JsonObject jsonFilter = new JsonParser().parse(filter).getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = jsonFilter.entrySet();
for(Map.Entry<String, JsonElement> entry : entrySet) {
String filterField = entry.getKey();
String filterValue = entry.getValue().toString().replace("\"","");
System.out.println("############");
System.out.println(filterField);
System.out.println(filterValue);
//if (filter.value)
if (!filterValue.equals("")) {
//Predicate pred = cb.like(root.get(filterField), "%" + filterValue + "%");
Predicate pred = cb.equal(root.get(filterField), filterValue);
predicateList.add(pred);
}
}
Predicate allPredicates = cb.and(predicateList.toArray(new Predicate[0]));
return allPredicates;
}