-->
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.  [ 5 posts ] 
Author Message
 Post subject: Duplicate entity
PostPosted: Mon Jan 29, 2007 4:48 am 
Newbie

Joined: Wed Sep 20, 2006 7:42 am
Posts: 12
Location: Sweden
Hi!

I have a rather large dm object that I would like to duplicate. The dm in question is an article object and an article is placed in one or more categories.

I have implemented the clone method in the article class:

Code:
public Object clone(){

Article art = new Article();
art.setCategories(this.getCategories());

return art;

}


And my actual clone code looks like this:

Code:
ArticleHome ahome = new ArticleHome()
Article art = ahome.findById(id);
Article nart = (Article)art.clone();
ahome.persist(nart);


Not surprisingly, this doesn't work. I get:

ArticleHome:49 - persist failed

org.hibernate.HibernateException: Found shared references to a collection: com.ian.dm.Article.categories

How do I get around this?

All the best!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 5:40 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
set the primary key to null

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 6:34 am 
Newbie

Joined: Wed Sep 20, 2006 7:42 am
Posts: 12
Location: Sweden
Ok, but that doesn't solve my problem in a convient way. I have more then one collection mapped to an article, and then I have to make duplicates of every object that is a part of the collection; first for every Category in the collection and then for every other object type that are a part of a collection in my article object.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 6:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no you just have to copy the collection; not the elements! (assuming it is a manytomany relationship you have)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 9:02 am 
Newbie

Joined: Wed Sep 20, 2006 7:42 am
Posts: 12
Location: Sweden
Ok!

Now, I don't get any errors but the categories aren't copied.

How do I make a copy of the collection? Does the following work:

Code:
Set<Category> d= new HashSet<Category>();
d.addAll(this.getCategories());


My hbm-file looks like this:

Code:
 
<set name="articles" table="category_article" cascade="all" inverse="true" lazy="true" order-by="(article) desc">
<key column="category"/>
<many-to-many column="article" class="com.ian.dm.Article"/>
</set>


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