如何使用 JPA Criteria API 指定左连接的多个条件?
我想将以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建联接后尝试调用
cq.select(r);
。如果没有cq.select()
,则最后一次cq.from()
调用的结果将用作选择根。Try invoking
cq.select(r);
after creating a join. Withoutcq.select()
the result of the lastcq.from()
call is used as a selection root.