Database Trigger and Hibernation

I came across a script:

  • save or update some data in the target table using hibernate
  • there is a trigger in the target table that will be executed before the insert or update operations of the target table
  • select output this entry using hibernate

But I find that the fields that were changed using the trigger are not actually selected. Is this related to commit transactions Hibernate (flush () is already being called) or Hibernate cache? thanks.

+6
source share
2 answers

This can be caused both by the cache of the first (session), and the second (for example, ehcache). To re-read the object, you need to call session.refresh ().

From hibernate docs (bottom of section)

You can reload the object and all its collections at any time using the refresh () method. This is useful when database triggers are used to initialize some properties of an object.

 sess.save(cat); sess.flush(); //force the SQL INSERT sess.refresh(cat); //re-read the state (after the trigger executes) 
+4
source

You can display properties as generated values . These values ​​always come from the database and cannot be saved. Hibernate automatically loads these values ​​in the next query after inserting or updating the database.

+9
source

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


All Articles