Deep Copy JPA Objects

I need to make a deep copy of the object. Basically, there is a domain object that I need to replicate, so we need to make a deep copy of the entity and its child and child child in the near future. Then I need to set the id of everyone in the structure to null, so they will be inserted as new entities. There is already a discussion here and link text . The basic idea is to write deep copy logic yourself. I'm just wondering if there are other more efficient approaches like doing some procedures at the database level.

I use hibernate as a JPA provider, so dormant solutions will also work.

+6
hibernate jpa
source share
1 answer

In Java, there is no easy way to deeply clone objects; therefore, Hibernate has no specific support for this.

This suggests that you can access the Hibernate annotations from your deep copy code and use them to determine what to do - you can even add your own annotations (to stop the deep copy code by cloning static master data).

Thus, it should be possible to write an implementation that works this way, and uses the @Id annotation and some of your encoding rules to make this happen.

Using stored procedures may also work, depending on the database you are using and how much you match SQL. But the resulting code will be difficult for the core, testing and understanding. Therefore, I propose against this.

+1
source share

All Articles