-->
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.  [ 4 posts ] 
Author Message
 Post subject: how can i clone (duplicate) an oject?
PostPosted: Mon Apr 18, 2005 7:14 am 
Beginner
Beginner

Joined: Tue Apr 05, 2005 12:09 pm
Posts: 48
Location: Slovakia (SK), Košice (KE)
Hello

I am using Hibernate 3.0 and I have a problem duplicating (deep cloning) an object. Suppose me having Something object that contains OtherThing object. Both objects are stored in separate tables and OtherThing is mapped to Something with cascade="all".

Code:
Something something = (Something) session.load(Something.class, somethingId);
session.evict(something);
something.setId(null);

OtherThing ot = something.getOtherThing();
od.setId(null);

session.save(something);


This code throws an exception:
Code:
AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null identifier
   at org.hibernate.engine.EntityKey.<init>(EntityKey.java:33)
   at org.hibernate.engine.PersistenceContext.reassociateProxy(PersistenceContext.java:489)
   at org.hibernate.engine.PersistenceContext.unproxyAndReassociate(PersistenceContext.java:528)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:65)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)


Please tell me how to clone object with all its children most effeciently (I don't wanna use object.clone() if possible). My real classes contains not only single objects but collections as well (one-to-many mappings ...).

Thanks a lot
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 2:38 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Did you find a better solution then .clone() ?
I had the same problem and solved it with the clone method ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 4:09 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
As I see it, it would be illegal (considering current session) to map the same object with two different persistent entities.
So the only way is to use plain Java clone().

You can probably hack it with evict().

What about replication between different two sessions?
Setting identifier to unsaved value before replication into 2nd session.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 24, 2005 3:20 pm 
Would replicate work?


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