3

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.

4
  • What's the mapping of UserStatus ? Commented Mar 14, 2013 at 15:29
  • I've updated the question with the userStatus mapping. There is nothing in there but for a couple of Strings, no object references. Commented Mar 14, 2013 at 15:38
  • Sorry, the hibernate version and the code of the query fetching data might also be of interest. Are you sure that it is hibernate that generated your sql ? Hibernate sql looks more like : ... from USER this_ inner join USER_STATUS userstatus1_ on this_.USER_STATUS_ID=userstatus1_.ID ... For me the userStatus was fetched eagerly by default (as for any *ToOne mapping), and with an inner join. Commented Mar 14, 2013 at 15:52
  • If UserStatus is not nullable in User then in your database you must have that "not null" constraint in place and every entry in the USER table will have a non-null value in the USER_STATUS_ID column. In that case then it does not really matter what kind of join is used. Commented Mar 28, 2013 at 13:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.