-->
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 to replicate an object in Hibernate??
PostPosted: Sat Aug 19, 2006 1:09 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.myproject.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 1:30 pm 
Newbie

Joined: Thu Mar 09, 2006 11:52 am
Posts: 13
When you read an object from the database, it is bound to the session and has the state persistent. You must not set the identifier property of a persistent object to null, cause then the binding to the database row gets messed up.

You have to instantiate a new (transient) object of your type and set all the properties except for the identifier property which you can then make persistent by a call to session.save(yourNewTransientObject).

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 21, 2006 2:09 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
i back firestarter :) u can have only one single instance of persistant object representing same data inside a session , so changing id of object will result in a complaint from hibernate :) . better u go with a transient object for the new data

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 8:49 am 
Newbie

Joined: Wed Dec 07, 2005 6:31 am
Posts: 9
Dunno if this helps, but I'm struggling with object replication myself.

I've had some limited success with

Code:
session.evict( myObject ); // detach object from session
myObject.setID( 0 );
session.replicate( myObject, ReplicationMode.OVERWRITE); // rewrite with new id


This produce a new identical row in myObject entity table (except for the new id), but related objects are now related to the new copy. This might be because I'm using annotations and haven't figured out how to cascade "REPLICATE" yet... Anyone know how?

OJ[/code]

_________________
OJ


Top
 Profile  
 
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.