-->
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.  [ 1 post ] 
Author Message
 Post subject: Strange SQL output with a many-to-many relationship
PostPosted: Mon Oct 19, 2009 5:33 pm 
Newbie

Joined: Mon Oct 19, 2009 4:32 pm
Posts: 3
hello everyone,

I'm quite new to Hibernate and I'm currently stuck on trying to achieve something that apears to be rather simple:
I can successfully get a bidirectional relationship between different kinds of objects but not when it involves 'joined-subclasses'.

Here is a description of the setup.

On one end I have 3 concrete classes (called classAAA, classAAB, classAAC). All of them inherit from an abstract class called classAA. And classAA inherits from the interface classA.
On the other end I have another concrete class, called classD. And classAA and classD perform a specific relationship and are bound together (through a many-to-many relationship).

Here are the hibernate mappings:
classA.hbm.xml
Code:
<hibernate-mapping>
  <!-- interface class classA -->
  <class name="domain.classA" table="classA" abstract="true">
    <id column="id" name="id">
      <generator class="assigned"/>
    </id>
    <!-- children of class classA -->
    <!-- abstract class classAA -->
    <joined-subclass name="domain.classAA" table="classAA" abstract="true">
      <key column="id"/>
      <set cascade="all" lazy="true" name="classDs" table="classAclassD">
        <key column="classAid"/>
        <many-to-many class="domain.classD" column="classDid"/>
      </set>
      <!-- children of classAA -->
      <!-- concrete classes -->
      <joined-subclass name="domain.classAAA" table="classAAA">
        <key column="id"/>
      </joined-subclass>
      <joined-subclass name="domain.classAAB" table="classAAB">
        <key column="id"/>
      </joined-subclass>
      <joined-subclass name="domain.classAAC" table="classAAC">
        <key column="id"/>
      </joined-subclass>
    </joined-subclass>
  </class>
</hibernate-mapping>


classD.hbm.xml
Code:
<hibernate-mapping>
  <class name="domain.classD" table="classD">
    <id column="label" name="label">
      <generator class="assigned"/>
    </id>
    <set cascade="all" lazy="true" name="classAs" table="classAclassD">
      <key column="classDid"/>
      <many-to-many class="domain.classAA" column="classAid"/>
    </set>
  </class>
</hibernate-mapping>


The SQL produced looks funky and is not valid when I try to gather all objects classAA linked to an object of classD when this portion is meant to be resolved:
Code:
<set cascade="all" lazy="true" name="classAs" table="classAclassD">
      <key column="classDid"/>
      <many-to-many class="domain.classAA" column="classAid"/>
</set>

Code:
    select
        classAs0_.classDid as classDid1_,
        classAs0_.classAid as classAid1_,
        classAA1_.id as id7_0_,
        case
            when classAA1_2_.id is not null then 2
            when classAA1_3_.id is not null then 3
            when classAA1_4_.id is not null then 4
            when classAA1_.id is not null then 1
        end as clazz_0_
    from
        classAclassD classAs0_
    left outer join
        classAA classAA1_
            on classAs0_.classAid=classAA1_.id
    left outer join
       
    classA classAA1_1_
        on classAA1_.id=classAA1_1_.id left outer join
            classAAA classAA1_2_
                on classAA1_.id=classAA1_2_.id
        left outer join
            classAAB classAA1_3_
                on classAA1_.id=classAA1_3_.id
        left outer join
            classAAC classAA1_4_
                on classAA1_.id=classAA1_4_.id
        where
            classAs0_.classDid=?

this section of the SQL code:
Code:
left outer join
       
    classA classAA1_1_
        on classAA1_.id=classAA1_1_.id

is the culprit and shouldn't be present.

It is worth noting that also tried to access the superclass classA (and not classAA) from within classD and changed this:
Code:
<many-to-many class="domain.classAA" column="classAid"/>

into that:
Code:
<many-to-many class="domain.classA" column="classAid"/>

but the SQL is still invalid with the same unwanted section.

Can you please help understanding what's happening? I'd be glad if I can get a helping hand on this matter because I really don't understand why it cannot work that way.

Thanks in advance.


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

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.