• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Hibernate - Attempting to duplicate(copy) an entity

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to duplicate an object that has child relationships. Performing psuedo-code similair to :



fails to work correctly - it appears to prepare for it but then fails to execute any insert statements.

What is the proper way to duplicate an entity with dependant relationships (eg: children collections) to the same database?
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about evicting the entity, setting the id to zero, and then updating it. Hibernate should treat it as a new entity, and add a new record. You may need to make sure you're not using lazy loading to get the associated objects.

What do you think?

-Cameron McKenzie
 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually after further google-fu I finally found the folling url :
http://forum.hibernate.org/viewtopic.php?p=2246023&sid=b9eaa0266d7915563431127d5964355d

Making use of BeanUtils.copyProperties to copy into a seperate entity seemed to work (I did need to manually update collection references as they pointed to the old object).

I'm a bit surprised in having to implement things in this manner. I would have thought hibernate, from an entity perspective would allow the basic operations on them (including copy).
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic