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