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.  [ 3 posts ] 
Author Message
 Post subject: Self Referential Many to Many Problem
PostPosted: Wed Nov 12, 2008 2:23 am 
Newbie

Joined: Tue Nov 11, 2008 3:18 pm
Posts: 2
Hi,

I have an Entity by the name of SubCity. I need to maintain SubCities which are close by. For Example SubCity 'A' is close by to SubCity 'B' and 'C'. And SubCity 'B' is close by to SubCity 'A' and 'D'.
Thus this is a self referencing many-to-may relationship.

Hence my SubCity class has two SetS (java.util) of SubCities. One which holds the SubCities which are close by to this SubCity and the other which holds the SubCities to which this SubCity is close by to.

My Hibernate mapping file is as follows.

Code:
<hibernate-mapping>
   <class name="SubCity" table="SUBCITY">
      
      <id name="id" type="long">
         <column name="ID" length="10"></column>
         <generator class="native"></generator>
      </id>
      
      <version column="VERSION" name="version" type="long" />
      
      <property name="code" type="string" length="10">
         <column name="CODE" length="10"></column>
      </property>
      
      <property name="name" type="string">
         <column name="NAME" length="50"></column>
      </property>
      
      <many-to-one name="city" class="City" lazy="false">
         <column name="CITY_CODE" length="10"></column>
      </many-to-one>
      
      <set name="subCitiesInProximity" inverse="true" lazy="false" table="SUBCITY_CLOSESUBCITY">
         <key>
            <column name="HOST_ID" length="10"></column>
         </key>
         <many-to-many class="SubCity">
            <column name="ASSOCIATE_ID" length="10"></column>
         </many-to-many>
      </set>
      
      <set name="subCitiesToProximity" inverse="true" lazy="false" table="SUBCITY_CLOSESUBCITY">
         <key>
            <column name="ASSOCIATE_ID" length="10"></column>
         </key>
         <many-to-many class="SubCity">
            <column name="HOST_ID" length="10"></column>
         </many-to-many>
      </set>
      
   </class>
</hibernate-mapping>


I try to save the sub cities as bellow.

Code:
public void setProximity(SubCity subCity1, SubCity subCity2){
      if (log.isDebugEnabled()) {
         log.debug("Going to set Proximity of Sub Cities " + subCity1.getCode() + ", " + subCity2.getCode());
      }
      
      //Hibernate Session.
      Session session = getSession(false);
      try {
         subCity1.getSubCitiesInProximity().add(subCity2);
         subCity2.getSubCitiesToProximity().add(subCity1);
         
         session.merge(subCity1);
         session.merge(subCity2);
      } catch (HibernateException ex) {
         throw convertHibernateAccessException(ex);
      }
   }


My problem is that when the above code is run. Although the version's of the SubCities are getting updated, nothing is inserted into the many-to-many relationship resolving table (SUBCITY_CLOSESUBCITY)

If there is any one who can identify my problem please do reply soon.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 5:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You have mapped both sets with inverse="true". This means that Hibernate will not use the sets for updating the database. You should remove the inverse="true" from one of the sets.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 11:57 am 
Newbie

Joined: Tue Nov 11, 2008 3:18 pm
Posts: 2
Thank You. I've solved my problem. I can solve my Problem by only using 1 Set. I removed the other one.

Thanks Anyway :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.