Suppose, we have 3 entities
{code:xml} <hibernate-mapping> <class name="AEntity" table="A" abstract="true"> <id name="id" > < column name ="ID"/> </id>
<joined-subclass table="Aa" name="AaEntity"> <key column="key"/> <many-to-one name="b" class="BEntity"> <column name="B_ID"/> </many-to-one> </joined-subclass>
<joined-subclass table="Ab" name="AbEntity"> <key column="key"/> <many-to-one name="b" class="BEntity"> <column name="B_ID"/> </many-to-one> </joined-subclass> </class> </hibernate-mapping> {code} {code:xml} <hibernate-mapping> <class name="Parent" table="parent"> <id name="id"> <column name="ID"/> </id> <many-to-one name="a" class="AEntity"> <column name="a_ID" /> </many-to-one> </class> </hibernate-mapping> {code} {code:xml} <hibernate-mapping> <class name="BEntity" table="B"> <id name="id"> <column name="ID"/> </id> </class> </hibernate-mapping> {code}
Then, we make criteria request {code:java} Criteria criteria = session.createCriteria(Parent.class); criteria.createAlias("a","a"); criteria.createAlias("a.b","b"); criteria.list(); {code} And get next sql {code:sql} 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 {code} 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? |
|