Child表上的Hibernate Criteria API [英] Hibernate Criteria API on Child table

查看:172
本文介绍了Child表上的Hibernate Criteria API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是2 DTO:

  class Item {
private Integer id;
私人整数serviceId;
私人字符串itemGuid;
private String meterId;
private Integer resourceId;
private String meterName;
私有字符串描述;
私有字符串类别;
private String subCategory;
私人列表费率;
}
public class Rates {
private Integer id;
私人双人单位;
私人双重价格;
private String field1Unique;
私人字符串field2Unique;
private String field1Index;
private String field2Index;
private String serviceData;
}

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

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

,在Rates表中,连接列在下面: @JoinColumn(name =Item_id,nullable = false) p>

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



你能告诉我,我该如何使用标准API来获取?

应该是像这样:

  CriteriaBuilder cb = null; 
CriteriaQuery< Item> cq = cb.createQuery(Item.class);
Root< Item> root = cq.from(Item.class);
加入<商品,费率> join = root.join(rates);
cq.where(cb.and(cb.equal(root.get(serviceId),10),cb.equal(join.get(field1Index),24)));


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

解决方案

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

这篇关于Child表上的Hibernate Criteria API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆