I have to do a deep copy of an entity. Basically there is a domain object whom I have to replicate so we need to do the deep copy of the entity and its child and child of child and soon. Than I need to set the id of everyone in the structure as null, so they get inserted as new entities. There is already some discussion here and link text. The basic idea is to write a deep copy logic on our own. I am just wondering if there are other better approaches like doing some procedures at the database level itself.
I am using hibernate as JPA provider, so hibernate specific solutions will also work.
There is no easy way to deep clone objects in Java; therefore Hibernate has no specific support for this.
That said: You can access the Hibernate annotations from your deep copy code and use that to figure out what to do -- you can even add your own annotations (to stop the deep copy code cloning static master data).
So it should be possible to write an implementation that works this way and uses the @Id
annotation and some of you coding rules to make this happen.
Using stored procedures might also work, depending on which database you use and how fit you are in SQL. But the resulting code will be hard to main, test, and understand. Therefore I suggest against it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With