4

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 Answers3

1

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);
sanya
  • 860
  • 1
  • 8
  • 20
0

If you have a version attribute i.e. annotated with @Version you could use OPTIMISTIC_FORCE_INCREMENT for loading the entity which will always cause a write that increments the version: https://docs.oracle.com/javaee/7/api/javax/persistence/LockModeType.html#OPTIMISTIC_FORCE_INCREMENT

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
  • thanks for response. If I have a version column, of course, the records implicitly becomes dirty. Also, I need to apply this change across the application. – harsha chakra Nov 19 '20 at 14:22
0

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.

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
  • Thanks for the response. The use case is very simple to reproduce here. Find entities from DB table, converting into user objects `List entityList = repository.findAll();` User would update these and send back. Once again, we convert these objects into entities and update to DB `updatedMyEntityList = repository.saveAll(updatedMyEntityList);` If there were no changes in the list provided, Hibernate is not firing the UPDATE query, which is understandable as its not required. But due to some DB triggers and all, we want to fire the UPDATE query even if there are no changes. – harsha chakra Nov 23 '20 at 14:56