• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

creating copies, JPA

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic