• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating copies, JPA

 
Antero Laukkanen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this complex object graph I need to be able to persist and more problematically, copy it once it has been persisted so that I can insert the copy also so that changes to it are not visible to the original graph. How to?

Using SerializationHelper in org.hibernate.utils I can create a deep-copy of the object graph except I think the lazily loaded properties are not initialized (?). Supposing I could get the full copy, however, the id's would still be the same as for the originals. To insert the object again, would setting the id's to default values be enough or will there be issues because the object is the proxy?

All in all, am I going the totally wrong way with this?
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Antero Laukkanen:
I have this complex object graph I need to be able to persist and more problematically, copy it once it has been persisted so that I can insert the copy also so that changes to it are not visible to the original graph. How to?

Using SerializationHelper in org.hibernate.utils I can create a deep-copy of the object graph except I think the lazily loaded properties are not initialized (?). Supposing I could get the full copy, however, the id's would still be the same as for the originals. To insert the object again, would setting the id's to default values be enough or will there be issues because the object is the proxy?

All in all, am I going the totally wrong way with this?

Hi Antero,

Not sure exactly what you are trying to do, but you should not use vendor-specific or Hibernate-specific API if you can avoid it, and in this case you can. Simply use the merge() method, which returns managed copies of the objects that you want to persist. The original objects are not managed, hence you can change them and merge them in later if you want.
 
Antero Laukkanen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply..

You are right about using the SerializationHelper That was not good thinking by me..

What I wanted to do was to create a copy of an object graph that is only in the db. I actually gave up on this because I could not figure out how to do this..

Just a quick follow-up question on what you said though. Yu suggest calling merge to persist the object? Do I not necessarily need to be persist the object first to make it managed..?

Thanks for your time..
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick follow-up question on what you said though. Yu suggest calling merge to persist the object? Do I not necessarily need to be persist the object first to make it managed..?

Nope. Merge will persist it if it does not already exist.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic