问题
Here is the 2 DTO:
class Item {
private Integer id;
private Integer serviceId;
private String itemGuid;
private String meterId;
private Integer resourceId;
private String meterName;
private String description;
private String category;
private String subCategory;
private List rates;
}
public class Rates {
private Integer id;
private Double unit;
private Double price;
private String field1Unique;
private String field2Unique;
private String field1Index;
private String field2Index;
private String serviceData;
}
The relationship between Item and Rates is given below :
@OneToMany(mappedBy = "ItemDTOs", fetch = FetchType.LAZY)
and in Rates table the join column is below : @JoinColumn(name = "Item_id", nullable = false)
I need to fetch all the Items along with Rates where Item.serviceId=10(from item table ) and Rates.field1Index=24 (From Rates table)
Can you tell me please how can I fetch that using criteria API
回答1:
Should be something like that:
CriteriaBuilder cb=null;
CriteriaQuery<Item> cq = cb.createQuery(Item.class);
Root<Item> root = cq.from(Item.class);
Join<Item, Rates> join = root.join("rates");
cq.where( cb.and(cb.equal(root.get("serviceId"), 10), cb.equal(join.get("field1Index"),24)));
来源:https://stackoverflow.com/questions/38251392/hibernate-criteria-api-on-child-table