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.