简体   繁体   English

使用条件构建器忽略了JPA Hibernate生成的左联接

[英]JPA Hibernate Generated Left Join ignored using criteria builder

I have an entity (Person) having a @ManyToOne relation with another entity (Availability) and other entities. 我有一个与另一个实体(可用性)和其他实体具有@ManyToOne关系的实体(人)。 When I get the Persons, I don't have the persons where the Availability is Null as Hibernate do an inner join (If I have an eager fetch), or a Select if Lazy fetch. 当我获得人员时,我没有可用性为Null的人员,因为Hibernate进行内部联接(如果我渴望获取),或者没有Select(如果是惰性获取)。 In the same time, I try to create another bean from the result so I use: 同时,我尝试从结果中创建另一个bean,因此我使用:

query.select(builder.construct(MyPerson.class,root.get("availability").get("date").....)
This will generate 
   select a.date, ... from Person p, Availability a Where p.availId = a.id.
As I need a Left join, I have added to my code :
   Join<Availability, person> avail = root.join("availability", JoinType.LEFT);

Strange, it will generate an LEFT OUTER join but still use the old request 奇怪,它将生成一个LEFT OUTER join但仍使用旧请求

 select **a2**.date, ... 
   from Person p, 
   LEFT OUTER JOIN Availability a1 on a1.id = p.availId
   ,**Availability a2** 
   **Where p.availId = a2.id.** 

What is wrong with it? 怎么了 The Only case when it generate only the Left Join is when I construct the new bean with the root. 当它仅生成Left Join时,唯一的情况是用根构造新bean。 (with a Lazy Loading), but it will generate too many other queries. (带有延迟加载),但它将生成太多其他查询。

 query.select(builder.construct(MyPerson.class,root)

Finally, I find the Solution . 最后,找到解决方案 In fact, I don't need to use Join avail = root.join("availability", JoinType.LEFT); 实际上,我不需要使用Join avail = root.join(“ availability”,JoinType.LEFT);

So I have removed it, and while creating my new bean I do : query.select(builder.construct(MyPerson.class, root.join("availability", JoinType.LEFT).get("date")) 因此,我已将其删除,并在创建新bean时执行了以下操作:query.select(builder.construct(MyPerson.class,root.join(“ availability”,JoinType.LEFT).get(“ date”))

Now I have only one generated query with the LEFT OUTER JOIN . 现在我只有一个使用LEFT OUTER JOIN生成的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM