如何强制hibernate从数据库加载数据?
休眠问题:
我使用源代码中的本机查询更新表“数据”中的列“计数”。后来我想加载表“data”中的所有行,但列“count”的值没有改变。
表“数据”中的行加载:
... .createQuery("来自数据") .setCacheable(false)
。列表();
知道出了什么问题吗?
Problem with hibernate:
I update a column "count" in a table "data" with a native query in my source code. Later I want to load all rows from table "data" but the value of column "count" has not changed.
Rows from table "data" are loaded with:
...
.createQuery("from data")
.setCacheable(false)
.list();
Any idea what's wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setCacheable(false)
没有用(它是默认值)。它所指的缓存就是二级缓存。在执行更新查询之前,您可能已经在会话中加载了一些实体(即第一级缓存)。在这种情况下,选择查询将针对数据库执行,但将从会话中返回已加载的实体。因此,您需要 驱逐 在执行选择查询之前从会话中取出这些实体,或者完全清除会话。如果实体缓存在二级缓存中,那么您还必须将它们从二级缓存中逐出。请参阅getCache。
setCacheable(false)
isn't useful (it's the default). The cache it refers to is the second-level cache. You probably have loaded some entities in the session (i.e. the first-level cache) before executing your update query. In this case, the select query will perform against the database, but will return the already-loaded entities from the session. You thus need to evict these entities from the session before executing the select query, or to completely clear the session.If the entities are cached in the second-level cache, then you also have to evict them from the second-level cache. See getCache.