简体   繁体   中英

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. 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. In the same time, I try to create another bean from the result so I use:

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

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

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

Now I have only one generated query with the LEFT OUTER JOIN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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