如何使用 JPA Criteria API 指定左连接的多个条件?

发布于 2024-09-10 07:24:21 字数 2175 浏览 2 评论 0原文

我想将以下 SQL 查询转换

select * from region_tree country left outer join region_tree region   
on country.REG_CODE_PAR=region.REG_CODE  
and region.LFT < country.LFT   
and region.RGT > country.RGT  
and region.REG_CODE_PAR = 'ALL'  
and COUNTRY.STATUS_CODE = 'A'  
and REGION.STATUS_CODE = 'A  

为基于 JPA Crtieria 的查询。

我创建了一个实体来表示自连接:

@Entity  
@Table(name = "REGION_TREE")  
public class RegionTree implements Serializable {  
    ... some other attributes  

    @ManyToOne  
    @JoinColumn(name = "REG_CODE_PAR")  
    private RegionTree region;   

    ... getters and setters  
}

我使用以下代码来创建 JPA 查询

CriteriaBuilder cb = em.getCriteriaBuilder();  
CriteriaQuery<RegionTree> cq = cb.createQuery(RegionTree.class);  
Root<RegionTree> e = cq.from(RegionTree.class);  
Join<RegionTree, RegionTree> r = e.join("region", JoinType.LEFT);  
Predicate p1 = cb.greaterThan(e.get("lft").as(Integer.class), r.get("lft").as(Integer.class));  
Predicate p2 = cb.lessThan(e.get("rgt").as(Integer.class), r.get("rgt").as(Integer.class));  
Predicate p3 = cb.equal(e.get("statusCode"), "A");  
Predicate p4 = cb.equal(r.get("statusCode"), "A");  
Predicate p5 = cb.equal(r.get("regCodePar"), "ALL");  
cq.where(p1,p2,p3,p4,p5);  
TypedQuery<RegionTree> tq = em.createQuery(cq);  
l = tq.getResultList();`

这是当我运行这段代码时 Hibernate 自动生成的查询。

select  
regiontree0_.REG_CODE as REG1_7_,  
regiontree0_.LFT as LFT7_,  
regiontree0_.NAME as NAME7_,  
regiontree0_.REG_CODE_PAR as REG4_7_,  
regiontree0_.RGT as RGT7_,  
regiontree0_.STATUS_CODE as STATUS6_7_   
from  
REGION_TREE regiontree0_   
left outer join  
REGION_TREE regiontree1_   
on regiontree0_.REG_CODE_PAR=regiontree1_.REG_CODE   
where  
cast(regiontree0_.LFT as integer)>cast(regiontree1_.LFT as integer)   
and cast(regiontree0_.RGT as integer)<cast(regiontree1_.RGT as integer)   
and regiontree0_.STATUS_CODE=?   
and regiontree1_.STATUS_CODE=?   
and regiontree1_.REG_CODE_PAR=?  

我尝试了多种方法,包括删除 cq.where 代码行,但生成的查询与我原来的查询不匹配。我配置有什么错误吗?

I'd like to convert the following SQL query:

select * from region_tree country left outer join region_tree region   
on country.REG_CODE_PAR=region.REG_CODE  
and region.LFT < country.LFT   
and region.RGT > country.RGT  
and region.REG_CODE_PAR = 'ALL'  
and COUNTRY.STATUS_CODE = 'A'  
and REGION.STATUS_CODE = 'A  

into JPA Crtieria based query.

I created an entity to represent the self join:

@Entity  
@Table(name = "REGION_TREE")  
public class RegionTree implements Serializable {  
    ... some other attributes  

    @ManyToOne  
    @JoinColumn(name = "REG_CODE_PAR")  
    private RegionTree region;   

    ... getters and setters  
}

I used the following code to create the JPA query

CriteriaBuilder cb = em.getCriteriaBuilder();  
CriteriaQuery<RegionTree> cq = cb.createQuery(RegionTree.class);  
Root<RegionTree> e = cq.from(RegionTree.class);  
Join<RegionTree, RegionTree> r = e.join("region", JoinType.LEFT);  
Predicate p1 = cb.greaterThan(e.get("lft").as(Integer.class), r.get("lft").as(Integer.class));  
Predicate p2 = cb.lessThan(e.get("rgt").as(Integer.class), r.get("rgt").as(Integer.class));  
Predicate p3 = cb.equal(e.get("statusCode"), "A");  
Predicate p4 = cb.equal(r.get("statusCode"), "A");  
Predicate p5 = cb.equal(r.get("regCodePar"), "ALL");  
cq.where(p1,p2,p3,p4,p5);  
TypedQuery<RegionTree> tq = em.createQuery(cq);  
l = tq.getResultList();`

This is the query automatically generated by Hibernate when I run this piece of code.

select  
regiontree0_.REG_CODE as REG1_7_,  
regiontree0_.LFT as LFT7_,  
regiontree0_.NAME as NAME7_,  
regiontree0_.REG_CODE_PAR as REG4_7_,  
regiontree0_.RGT as RGT7_,  
regiontree0_.STATUS_CODE as STATUS6_7_   
from  
REGION_TREE regiontree0_   
left outer join  
REGION_TREE regiontree1_   
on regiontree0_.REG_CODE_PAR=regiontree1_.REG_CODE   
where  
cast(regiontree0_.LFT as integer)>cast(regiontree1_.LFT as integer)   
and cast(regiontree0_.RGT as integer)<cast(regiontree1_.RGT as integer)   
and regiontree0_.STATUS_CODE=?   
and regiontree1_.STATUS_CODE=?   
and regiontree1_.REG_CODE_PAR=?  

I've tried a number of ways including removing the cq.where line of code but the generated query can't match my original one. Have I configured anything wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-09-17 07:24:21

创建联接后尝试调用cq.select(r);。如果没有 cq.select(),则最后一次 cq.from() 调用的结果将用作选择根。

Try invoking cq.select(r); after creating a join. Without cq.select() the result of the last cq.from() call is used as a selection root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文