简体   繁体   English

用于SQL本机查询的Hibernate乐观锁定

[英]Hibernate optimistic locking for SQL native queries

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

Yes. 是。 You can construct a native SQL query, obtaining a SQLQuery instance from the session. 您可以构造本机SQL查询,从会话中获取SQLQuery实例。 Use addEntity to tell hibernate the type of entity that is returned by the query. 使用addEntity告诉休眠查询所返回的实体的类型。 Hibernate will properly manage these entity instances that are returned. Hibernate将正确管理返回的这些实体实例。

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. Hibernate不了解本机查询,因此它不知道正在访问哪些表。 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. 解决缓存/刷新问题的方法是告诉SQLQuery实例访问哪些实体类型。 Use the addSynchronized* methods to tell hibernate what is going on in the query, and hibernate will do all the hard work. 使用addSynchronized*方法告诉hibernate查询中发生了什么,hibernate将完成所有艰苦的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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