-->
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: Duplicating collections
PostPosted: Thu Sep 18, 2008 2:32 am 
Newbie

Joined: Thu Sep 18, 2008 2:24 am
Posts: 1
Hi, I am using Spring 2.0.8, Hibernate 3.2.2GA and PostgresSQL 8, and I am having problems with duplicating collections.

I have two classes ClassA and ClassB with property Set<Attachment> attachments.

When I want to copy attachment list from classA to classB using java's:

classB.getAttachment().addAll(classA.getAttachments());

the original list in classA will get deleted from the database when I store the classB object:

session.saveOrUpdate(classB);

What is the "right" way to duplicate collections. I want to store both sets separately but classB's list having objects from classA's set.

I have mapped the Set<Attachment> like this using xdoclet:

* @hibernate.key column="class_a_id"
* @hibernate.many-to-many class="my.example.Attachment" column="attachment_id"
* @hibernate.set
* table = "class_a_attachments"
* sort = "natural"

Thank you for reading.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2008 3:09 am 
Beginner
Beginner

Joined: Tue Sep 09, 2008 5:42 am
Posts: 22
Location: Romania
Ciao,

First of all I use Nhibernate so you need to try this with Hibernate to see how it works. But I will give you a general suggestion ...

I think the first thing you should do is after retreiving the list from classA is to create a method that makes a clone of the list but be careful to change the ID(key) to reflect the class they belong to. Try saving after that and see if anything changes.

Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 9:23 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Hi Yen,

Can you give more details on the structure of your ClassA, ClassB and Attachment are you using bi-directional mapping or uni-directional.

Code:
classB.getAttachment().addAll(classA.getAttachments());


may be it will add the references to classB might not be new Attachment objects.

_________________
Vinay N


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.