I am following the hibernate documentation for one-to-many and many-to-one Unidirectional associations with join tables.
I tried to implement a simple example by creating Person & Address entity like this:
Person has an id and name property with setters & getters
Address has an id and name property with setters & getters
I have the same mapping file given in the documentation for one-to-many and also many-to-one. Then I created a small program to get Person
entities and the corresponding Address
entities like this:
For one-to-many
:
for (Person person : list) {
System.out.println(person.getId());
for (Address address : person.getAddresses()) {
System.out.println(address);
}
}
For this hibernate generated below queries:
Hibernate: select person0_.personId as personId1_1_
from Person person0_
Hibernate: select addresses0_.personId as personId1_1_0_, addresses0_.addressId as addressId2_2_0_, address1_.addressId as addressId1_0_1_
from PersonAddress addresses0_
inner join Address address1_
on addresses0_.addressId=address1_.addressId
where addresses0_.personId=?
For many-to-one
:
List<Person> list = session.createQuery("from Person").list();
for (Person person : list) {
System.out.println(person.getId());
System.out.println(person.getAddress());
}
Hibernate: select person0_.personId as personId1_1_, person0_.name as name2_1_, person0_1_.addressId as addressId2_2_
from Person person0_
left outer join PersonAddress person0_1_
on person0_.personId=person0_1_.personId
Hibernate: select address0_.addressId as addressId1_0_0_, address0_.name as name2_0_0_
from Address address0_
where address0_.addressId=?
From the generated queries, for one-to-many
initially the query was to get records from Person
table to get all Persons then for getting the addresses it had JOIN between PersonAddress
and Address
.
Where as for many-to-one
initially it had LEFT OUTER JOIN between PersonAddress
and Address
to get Person
records then it hit the Address
table to get the records from Address
table.
So my doubt is why the many-to-one
case has not followed the same approach of one-to-many
by hitting only Person
table because initially I am trying to fetch only Person entities in my HQL. Please help me in understanding this.