I have something like this:
@Entity
public class User implements Serializable {
...
public User(String login, Division division) {
this.login = login;
this.division = division;
}
@ManyToOne
public Division getDivision() {
return division;
}
...
JQPL:
I'm using a custom constructor here because I don't want the query returning the user's password (and others attributes).
SELECT new com.xyz.app.beans.User(u.login, u.division)
FROM User u
WHERE u.login = :login
SQL generated:
select
user0_.login as col_0_0_,
user0_.division_id as col_1_0_
from
dbo.User user0_
inner join
dbo.Division division1_
on user0_.division_id=division1_.id
where
user0_.login=?
Obviously, if Division is null, my query will return an empty list or throw a NoResultException. I want the query to return the User regardless if he has a division or not.
There is a way to avoid this? How to tell to Hibernate to do a LEFT JOIN instead of an INNER JOIN?
Since you use u.division
in the select
clause, it implicitly converts to inner join. To include also users without the division, use this
SELECT new com.xyz.app.beans.User(u.login, d)
FROM User u left join u.division d
WHERE u.login = :login
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.