Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate and SQL join types

I have a simple JPA entity (hibernate implementation) with an eagerly fetched relationship:

User.java:

@Entity
@Table(name = "USER")
public class User {

    @OneToOne(optional=false)
    @JoinColumn(name = "USER_STATUS_ID", nullable = false)  
    private UserStatus userStatus;

UserStatus:

@Entity
@Table(name = "USER_STATUS")
public class UserStatus {

    @Column(name = "name", nullable = false)
    private String name;

and the query that is being generated to lookup the entity looks something like this:

...from user left outer join user_status on...

My question is: why is Hibernate performing this outer join and not an inner join (since I've told it that it's not nullable or optional). Is there any way to force an inner-join?

Thanks.

like image 326
StuPointerException Avatar asked Jun 27 '26 12:06

StuPointerException


1 Answers

If i am right, Hibernate always use left outer join and you cant force hibernate to do a inner join. When you retrieve parent entity, its not mandatory that all parent entities to have child. So if inner join is used you may end up without results.

like image 194
Meiyappan Kannappa Avatar answered Jun 29 '26 04:06

Meiyappan Kannappa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!