Hibernate Criteria API on Child table

蹲街弑〆低调 提交于 2019-12-12 03:22:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!