Persist detached entity


Sometimes you have to store an object in a database. I run into the problem that I got this error:

org.hibernate.PersistentObjectException: detached entity passed to persist

It’s realy easy to solve this problem, just ensure to create the object to be saved after beginning the transaction.
In my case I just created a constructor like this one:

public MyBean(MyBean entity){
setProp1(entity.getProp1);
setPop2(......
}

Then I created a new Onject to be saved just before the create statement to persist the object.

MyBean myBean = new MyBean(entity);
myDao.create(myBean);

, ,

  1. No comments yet.
(will not be published)
*