-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to replicate an object in Hibernate??
PostPosted: Sat Aug 19, 2006 1:08 pm 
Newbie

Joined: Fri Jul 28, 2006 9:52 am
Posts: 12
I have a parent table for eg: User_Mst is my table and the corresponding POJO object is UserMst.java.

I also have a child table which is User_Addr_Mst and the corresponding POJO object is UserAddrMst.java.

The assocciation between the parent and child is one to many, so Set of UserAddrMst is aggregated with the parent POJO(UserMst).

Now iam fetching a record from User_Mst table using the session.query and iam gettinga the parent object which consist of child set(UserAddrMst).

My requirement is to create another user with the same details as the object with which i have. which means i want to insert a new record with the details of the current object.

How do i do this?

I tried cloning the parent object and tried setting the primary key as null, but it throws me an exception..

Code:
org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=HP16003273432/46, BranchQual=, localId=46] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.HibernateException: identifier of an instance of com.mypackage.hibernate.UserMst altered from 4444 to null)


I have the alternate solution as callling an store procedure.. but is it possible with hibernate.. is there any other method to replicate an object..??


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 20, 2006 6:53 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The reason you got that particular exception is because you have to evict entities from the session at the same time as you set their IDs to null.

You can set the ID of all entities to null. Setting the ID of the UserMst to null is a start, but if you don't set the IDs of all UserAddrMsts to null too, you'll be trying to put existing addresses into two sets at once.. as you've mapped that as a one-to-many, you can't do that. So set all IDs of all entities that you're duplicating to null.

It is probably easier to do complex cloning like this in a stored procedure. The most correct way (IMO) would be to write a factory-type method for it: a deepClone that knows what entities it's cloning, and knows what to do with nested collections etc.

_________________
Code tags are your friend. Know them and use them.


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