-->
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: How to properly manipulate collections ?
PostPosted: Wed Nov 19, 2003 1:17 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 1:20 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Forgot to mention that in the end I will call

update(domain)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 8:35 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Gavin, could you please shed some light over what I described in this post?

TIA,

--steve p.


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.