开发者

Database trigger and Hibernate

开发者 https://www.devze.com 2023-03-26 09:32 出处:网络
I\'ve met a scenario: save or update some data in a target table by hibernate there is a trigger on the target table which will be executed before insert or update operations of the target table

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 reco开发者_运维问答rd 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.


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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消