Suppose, we have 3 entities
Code:
<hibernate-mapping>
<class name="BEntity" table="B">
<id name="id" column="ID"/>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="AEntity" table="A" abstract="true">
<id name="id" column="ID"/>
<joined-subclass table="Aa" name="AaEntity">
<key column="key"/>
<many-to-one name="b" class="BEntity" column="B_ID"/>
</joined-subclass>
<joined-subclass table="Ab" name="AbEntity">
<key column="key"/>
<many-to-one name="b" class="BEntity" column="B_ID"/>
</joined-subclass>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="Parent" table="parent">
<id name="id" column="ID"/>
<many-to-one name="a" class="AEntity" column="A_ID"/>
</class>
</hibernate-mapping>
Then, we make criteria request
Code:
Criteria criteria = session.createCriteria(Parent.class);
criteria.createAlias("a","a");
criteria.createAlias("a.b","b");
criteria.list();
And get next sql
Code:
select this_.ID as ID1_5_3_,
this_.a_ID as a2_5_3_,
a1_.ID as ID1_2_0_,
a1_.B_ID as B2_2_0_,
a1_1_.B_ID as B2_0_0_,
a1_2_.B_ID as B2_1_0_,
case when a1_1_.key is not null then 1 when a1_2_.key is not null then 2 when a1_.ID is not null then 0 end as clazz_0_,
b2_.ID as ID1_3_1_,
b2_.C_ID as C2_3_1_,
b2_.ID as ID1_3_2_,
b2_.C_ID as C2_3_2_
from parent this_
inner join A a1_ on this_.a_ID=a1_.ID
left outer join Aa a1_1_ on a1_.ID=a1_1_.key
left outer join Ab a1_2_ on a1_.ID=a1_2_.key
inner join B b2_ on a1_1_.B_ID=b2_.ID
inner join B b2_ on a1_2_.B_ID=b2_.ID
We can see that B table joins two times and have same name b2_. Is it bug? And how make two joins but with different name of B table. Okay?