• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - Attempting to duplicate(copy) an entity

 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic