-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate make same aliases for different joins
PostPosted: Tue Jul 05, 2016 4:47 pm 
Newbie

Joined: Tue Jul 05, 2016 4:34 pm
Posts: 1
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?


Top
 Profile  
 
 Post subject: Re: Hibernate make same aliases for different joins
PostPosted: Mon Jul 11, 2016 5:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
That's because you mapped the B entity twice:

Code:
       <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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.