Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 5006
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).
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic