简体   繁体   中英

Hibernate optimistic locking for SQL native queries

休眠是否通过SQL本机查询的显式版本概念支持乐观锁定?

Yes. You can construct a native SQL query, obtaining a SQLQuery instance from the session. Use addEntity to tell hibernate the type of entity that is returned by the query. Hibernate will properly manage these entity instances that are returned.

The other aspect that you need to be aware of is caching. Hibernate does not understand the native query, so it does not know which tables are being accessed. This means that hibernate cannot auto-flush the caches to ensure that the query sees the current state of the database. This leads to incorrect results that may be subtle and hard to identify.

The fix for the caching/flushing issue is to tell the SQLQuery instance which entity types are accessed. Use the addSynchronized* methods to tell hibernate what is going on in the query, and hibernate will do all the hard work.

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