Hi all,
I got the mappings:
<class name="com.db.device.CEAccessDomain" table="ceaccessdomain" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false">
<id name="id" type="long" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="name" column="name" type="string"/>
<property name="description" column="description" type="string"/>
<set name="sites" inverse="true" lazy="true" cascade="save-update">
<key column="domain_site_id"/>
<one-to-many class="com.db.device.NetPortalSite"/>
</set>
</class>
and
<class name="com.db.device.NetPortalSite" table="sites" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false">
<id name="id" type="long" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="siteName" column="site_name" type="string" not-null="true" unique="false"/>
<property name="description" column="description" type="string"/>
<many-to-one name="domain" class="com.db.device.CEAccessDomain" column="domain_site_id"/>
</class>
So I have a bidir one-to-many relationship b/w domain and sites. I want to remove/add sites to the domain association, but NOT to remove /add sites from/in the db (because sites are managed by other object).
What is the better way to code this?
If I try:
Code:
// update sites in the object
Iterator iter = domain.getSites().iterator();
while (iter.hasNext()) {
NetPortalSite s = (NetPortalSite)iter.next();
s.setDomain(null); // inverse relation, unlink from domain
}
iter = sites.iterator(); // NEW set of sites from GUI
while (iter.hasNext()) {
NetPortalSite s = (NetPortalSite)iter.next();
domain.getSites().add(s);
s.setDomain(domain); // inverse relation
}
it works, but end up adding to the site collection.
If I try:
Code:
// update sites in the object
Iterator iter = domain.getSites().iterator();
while (iter.hasNext()) {
NetPortalSite s = (NetPortalSite)iter.next();
s.setDomain(null); // inverse relation, unlink from domain
}
domain.getSites().clear(); // !!!!!!!!!!!!!!!!!!!!!!!
iter = sites.iterator(); // NEW set of sites from GUI
while (iter.hasNext()) {
NetPortalSite s = (NetPortalSite)iter.next();
domain.getSites().add(s);
s.setDomain(domain); // inverse relation
}
it will not update corectly when removing sites from colletion.
PLease help.
TIA,
--Steve p.