简体   繁体   中英

Hibernate should update only certain fields in the database

I have a database trigger which executes on insertion of data. From the application end, I use hibernate to update the same table which is update by the database TRIGGER. The data which was updated by Trigger gets overwritten by the older data when I update the data from the application end.I figured out that the cached value in hibernate is not in sych with the value from the database. Is there a proper way to get around this? The cache I am talking about is First level cache.

EDIT - Hibernate should update certain fields in the database and other fields in the database should be updated by the TRIGGER. Is this possible?

You can use refresh:

session.merge(entity);
session.flush();
//the trigger has been called
session.refresh(entity); 
//the entity will reflect the trigger changes

My issue was really what is mentioned in the EDIT of the question. The simple solution to the problem is as mentioned by Vlad in the comments. Mark the columns as @Column(name = "dob", insertable = false, updatable = false). This is help avoid any major surprises if hibernate updates the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM