-->
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: Endless state of bidirectional mapping
PostPosted: Tue Jul 22, 2008 8:17 am 
Newbie

Joined: Thu Jul 17, 2008 10:47 am
Posts: 2
Hello all,

I have posted before but it seems I have to rephrase due to no answer. Let me try this again:

I have two objects, Alpha and Beta.
Alpha has the following properties: id, name, Bset
Beta has the following properties: id, name Aset
They are mapped using biderectional many to many association using the following mappings:

alpha.hbm.xml
Code:
<class name="Alpha" table="ALPHA" dynamic-update="true"
      batch-size="10">
      <cache usage="read-write" />
      <id name="id" column="A_ID" node="id" />
   <property name="name" column="A_NAME" node="name" not-null="true" unique="true" />
   <set name="Bset" table="A_B_MAP">
           <key column="A_ID"/>
           <many-to-many class="Beta" column="B_ID"/>
       </set>
      
      <query name="findById">
         <![CDATA[select a from Alpha a where a.id=?]]>
      </query>

      <query name="findByName">
         <![CDATA[select a from Alpha a where a.name=?]]>
      </query>

<query name="findByBId">
         <![CDATA[select a from Alpha a left outer join a.Bset as bs where bs.id=?]]>
      </query>

   </class>


beta.hbm.xml
Code:
<class name="Beta" table="BETA" dynamic-update="true"
      batch-size="10">
      <cache usage="read-write" />
      <id name="id" column="B_ID" node="id" />
   <property name="name" column="B_NAME" node="name" not-null="true" unique="true" />
   <set name="Aset" table="A_B_MAP" inverse="true">
           <key column="B_ID"/>
           <many-to-many class="Alpha" column="A_ID"/>
       </set>
      
      <query name="findById">
         <![CDATA[select a from Alpha a where a.id=?]]>
      </query>

      <query name="findByName">
         <![CDATA[select a from Alpha a where a.name=?]]>
      </query>

<query name="findByBId">
         <![CDATA[select a from Alpha a left outer join a.Bset as bs where bs.id=?]]>
      </query>

   </class>


When I try to find an Alpha object using its unique id, to change the name for example, I use the following code:
Code:
Alpha a = null;
List myList = this.getHibernateTemplate().findByNamedQuery(Alpha.findById", id);
if (myList != null && myList.size() > 0)
     a= myList.get(0);


the sql I use is the following:

<query name="findById">
<![CDATA[select a from Alpha a where a.id=?]]>
</query>


This returns the required Alpha that includes the mapped Bset. No, each Bset of the ones returned has atleast the same Alpha in the Aset which again includes the same Bset bringing it in an endless state when I try to call that object (i.e. print it). If I try to change a property, the name for example, then I ll have to commit the whole database of Alphas and Betas that are mapped to each other.

Is there a way to keep the bidirectional mapping (so that I will be able to get the Bset of each A and the Aset of each B) but avoid entering this endless state?

Thank you
George


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.