3

I am following Hibernate documentation for bidirectional one-to-one relationship and created one-to-one mapping between Person and Address entities.

The mapping is like the one given in document:

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <many-to-one name="address" 
        column="addressId" 
        unique="true"
        not-null="true"/>
</class>

<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
   <one-to-one name="person" 
        property-ref="address"/>
</class>

Now I created s smll program to fetch the Person and the corresponding Address like this:

    List<Person> persons = session.createQuery("from Person where id=2").list();
    for (Person person : persons) {
        System.out.println(person);
        System.out.println(person.getAddress());
        System.out.println(person.getAddress().getPerson());
    }

For the line person.getAddress(), hibernate issues a LEFT OUTER JOIN query as:

select
        address0_.addressId as addressId1_0_0_,
        person1_.personId as personId1_1_1_,
        person1_.addressId as addressId3_1_1_ 
    from
        ADDRESS address0_ 
    left outer join
        PERSON person1_ 
            on address0_.addressId=person1_.addressId 
    where
        address0_.addressId=?

If a inner join is sufficient in this case then why hibernate uses left outer join? Please clarify.

learner
  • 6,062
  • 14
  • 79
  • 139
  • For the same reason as in your previous question: http://stackoverflow.com/questions/25756361/understanding-the-queries-generated-for-one-to-many-and-many-to-one-on-join-tabl. Hibernate is loading an address by ID, and has to know if the address has a person or not. – JB Nizet Sep 11 '14 at 05:56
  • @JBNizet, Thanks for responding to my post. But in both cases an inner join looks relevant in getting other entities data instead of left outer join then why hibernate prefers the latter? can you please help me in understanding this. – learner Sep 11 '14 at 07:01
  • Why would an inner join be relevant? How can Hibernate be sure that every address has a person? You've specified that every person has an address. That doesn't mean every address has a person. – JB Nizet Sep 11 '14 at 07:51
  • @JBNizet, Sorry for asking more doubts, as the query has a `where` condition I thought there is no use of having `LEFT OUTER JOIN` compared to `INNER JOIN` as the results remain same in both cases. Can you please tell me how `LEFT OUTER` is useful in this case? – learner Sep 11 '14 at 09:06
  • 1
    The where clause is used to load a given address by its ID. The left join is used to load the ID of the person of the address, if any. – JB Nizet Sep 11 '14 at 12:16
  • @JBNizet, Thanks a lot, now it is clear for me – learner Sep 11 '14 at 13:07

1 Answers1

5

Hibernate is loading the address using it's id (which it got from the PERSON). During the loading it is not sure whether the address has a person (it does not remember where it got the id from).

If the address had no person, and it used an inner join, no results would be returned. It uses outer join, so that if there is no person, a result will be returned with NULL in columns of the person.

This is what hibernate wants, so it uses outer join.

alexander zak
  • 929
  • 6
  • 13
  • can you please answer my other similar post --> http://stackoverflow.com/questions/25800160/why-the-one-to-many-bidirectional-mapping-with-join-table-uses-inner-and-left-ou – learner Sep 15 '14 at 06:10