Hibernate merge and flush do not persist changes to an object

Viewed 11829

This is a Hibernate/JPA question around updating an object in the database.

I have an object that I created in another transaction using entityManager.persist(object).

This object is returned and then some values are updated. A call then is made in another method that is annotated with @Transactional. This method calls a merge method in a base class (shown below).

In the following code, rvalue has the modified information up until refresh is called. Once refresh is called, the data is back to the original values.

I would expect that the call to flush() would persist the information to the database.

I tried commenting out the refresh call, thinking the save does not happen until I leave the method wrapped in transactional. However, I am still finding the changes are not being persisted.

Any ideas on what may be happening would be greatly appreciated!

@Override
public T merge(T object) {
    T rvalue = entityManager.merge(object);
    entityManager.flush();
    entityManager.refresh(rvalue);
    return rvalue;
}
3 Answers

I had this problem because I called session.setDefaultReadOnly(true); on the Hibernate Session.

Related