开发者

Deep copying of JPA entities [closed]

Closed. This question needs to be more focused. It is not currently acc开发者_运维技巧epting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

Improve this question

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜