子表上的Hibernate Criteria API

时间:2016-07-07 17:05:44

标签: java database hibernate orm hibernate-criteria

这是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; 
}

项目和费率之间的关系如下:

@OneToMany(mappedBy = "ItemDTOs", fetch = FetchType.LAZY)

并在费率表中,加入列位于以下位置:@JoinColumn(name = "Item_id", nullable = false)

我需要获取所有物品以及物品,其中Item.serviceId = 10(来自物品表)和Rates.field1Index = 24(来自物价表)

您能告诉我如何使用条件API

获取该信息

1 个答案:

答案 0 :(得分:2)

应该是这样的:

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)));