-->
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.  [ 6 posts ] 
Author Message
 Post subject: Making copies of objects
PostPosted: Thu Feb 05, 2009 10:41 am 
Newbie

Joined: Tue Oct 28, 2008 8:10 am
Posts: 5
Hibernate version:3

Hello,

I am trying to make a copy of an object and resave it as a new object.

I have tried everthing I can find but still get odd behavior(IE sometimes one will save properly, sometimes the changes will be in the old one saved etc.)

Basically I have an object with and ID, various fields, then an linkid.
I want to make a copy as it stands, remove the id and add it to the linkid and save, make changes to the original one and then save so I will have an exact copy of the object before the changes.

Thanks in advance,
--James


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 11:53 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
May be you can tell how u tried for this and what result u got.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 4:27 pm 
Newbie

Joined: Tue Oct 28, 2008 8:10 am
Posts: 5
Hello,

I have found one that seems to work, just wondering if there is anything wrong with copying them this way:

ObjectInputStream ois;
ObjectOutputStream oos;
ByteArrayInputStream bais;
ByteArrayOutputStream baos;
byte[] data;
Object copy;
// write object to bytes
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.close();
// get the bytes
data = baos.toByteArray();
// construct an object from the bytes
bais = new ByteArrayInputStream(data);
ois = new ObjectInputStream(bais);
copy = (Object) ois.readObject();
ois.close();
return copy;

--James


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 12:37 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
You can accomplish this with cloning also.
    Make your object cloneable
    clone the original one
    unset the id of the clone
    Make changes to original
    save both


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 9:00 am 
Newbie

Joined: Tue Oct 28, 2008 8:10 am
Posts: 5
You have to implement cloneable and I reverse eng my pojos from the database while developing and I havent figured out how to change the base design of the reverse eng of hibernate to include things like 'implements cloneable' etc.

--James


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 9:34 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
In that case your other solution should be fine.

_________________
Regards,
Litty Preeth


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