How to copy objects managed by Hibernate?

1.2k Views Asked by At

I want to copy an object into another (its properties). This object is managed by Hibernate and has lazy collections. PropertyUtils.copyProperties() throws lazy exception.

Is there any way?

PS: I do not want to unproxy my object since I don't own it (its in another jar)

1

There are 1 best solutions below

5
On

You can call Hibernate.initialize() on every lazy collectio of your original object before copying. Obviously, it would not work outside of a transaction, if yor object is detached from the Hibernate session. So, you have to initialize your lazy collections early enough.