Jpa merge unmanaged object

I would like to make an unmanaged entity manageable in a different persistence context. I read that this can be done by merging:

em.merge(user); 

But if I do this, it is not added to the context:

 boolean isManaged = em.contains(user); 

always false.

Even if I do the following:

 User dbuser = em.find(User.class, user.getId()); em.merge(user); boolean isManaged = em.contains(user); 

dbuser and user are exactly the same.

What am I doing wrong?

I am using JPA, MySql DB, JBoss EAP 6.1

+6
source share
1 answer

Call entityManager.flush() to commit the merge action to the database.

Usually the commit is delayed. For example, if your method has the @TransactionAttribute annotation. The transaction will be completed after the completion of the method. But if you call em.contains(user) without em.contains(user) , you just get the old state.

0
source

Source: https://habr.com/ru/post/957258/


All Articles