Hibernate: Always update a given record even if its not dirty

Viewed 446

My requirement is to update a table's record even though the record is not dirty. Hibernate is by default ignoring update query if all the columns that are passed are same. But I do not want that. Reason being, we have some triggers written on the database insert/update/delete, which needs to know if anyone trying to update any record. This is very critical element of the application and want it in this way.

I am browsing a lot on this, but finding about @DynamicUpdate, but what I want is the reverse of it.

Any help would be really helpful.

3 Answers

You need to use something like session.flush() after each update to force consequent updates perform "real" saving of an entity within same session/transaction. Can't advice more since you haven't provide any code sample.

You need to get a refernce to the Hibernate Session. Since you tagged JPA, you could use the EntityManager. Then you can evict the entity from the 1st level cache to disable change detection, and then run the update:

Session session = (Session)entityManager.getDelegate();  
session.evict(entity);  
session.update(entity);
Related