Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database trigger and Hibernate

I've met a scenario:

  1. save or update some data in a target table by hibernate
  2. there is a trigger on the target table which will be executed before insert or update operations of the target table
  3. select out this record by hibernate

But I find that the fields which have been modified by the trigger are not really fetched out. Is this related with transactions commit of Hibernate (flush() has already be called) or Hibernate cache? thanks.

like image 549
Li. Avatar asked Aug 08 '11 10:08

Li.


2 Answers

You can map properties as generated values. These values always come from the database and can't be stored. Hibernate automatically loads these values in a subsequent query after inserting or updating the database.

like image 115
Stefan Steinegger Avatar answered Oct 04 '22 03:10

Stefan Steinegger


This can be caused by both the first (session) or second (e.g. ehcache) caches. To re-read the entity, you'll need to call session.refresh().

From hibernate docs (at the bottom of the section)

It is possible to re-load an object and all its collections at any time, using the refresh() method. This is useful when database triggers are used to initialize some of the properties of the object.

sess.save(cat);
sess.flush(); //force the SQL INSERT
sess.refresh(cat); //re-read the state (after the trigger executes)
like image 20
Augusto Avatar answered Oct 04 '22 01:10

Augusto