Hibernate SQLQuery绕过hibernate会话缓存 [英] Hibernate SQLQuery bypasses hibernate session cache

查看:306
本文介绍了Hibernate SQLQuery绕过hibernate会话缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在事务化一些广泛的数据库操作,并且遇到了这个问题,如果我通过hibernate运行sql查询但未使用MQL方法,数据库视图看起来不正确。具体来说,代码在大多数情况下以更合适的方式使用hibernate,但有些地方有人决定只执行sql。我不喜欢他们这样做,但在这一点上这是它是什么。

我找到了解释,似乎可以解释它,但所有的例子都是实际获取和管理代码中的事务。我们在整个类上使用@TransactionAttribute注解来改变这段代码,并且发现了很多发生这种行为的地方,但我并不完全相信这个解释适用于简单包装在注释中的代码 - 我假设任何使用hibernate管理器的东西都会依赖于会话中的对象缓存。



如果我提到hibernate中的概念是由不正确的术语等引起的,解决方案

您的问题令人困惑,但我认为您在说当您执行本机时,Hibernate不会在会话缓存中查找实体查询。

SQL查询或本地查询是Hibernate刚刚转发到数据库的查询。 Hibernate不会解析查询,也不会解析结果。不过,Hibernate会让你处理结果,将列转换为类的属性。也就是说,Native Queries会绕过会话缓存听起来很自然。这是因为Hibernate不知道你的查询的任何内容,也不知道这个查询的结果(这不是那个对象)。

I'm "transactionalizing" some extensive database manipulation and I came across this issue where if I run sql queries through hibernate but not using the MQL approach, the view of the database doesn't appear correct. Specifically the code uses the hibernate in the more appropriate manner in most cases but there are places where someone decided to just execute sql. I don't like that they did this but at this point "it is what it is".

I found an explanation that seems to explain it but all the examples are wrt actually getting and managing the transaction in the code. We are using the @TransactionAttribute annotation on the entire class to change this code and are finding a lot of places where this behavior happens but I'm not entirely convinced the explanation applies to code that is simply wrapped in the annotation--I was assuming that anything using the hibernate manager would rely on the object cache in the session.

Apologies in advance if I am referring to concepts in hibernate by incorrect terminology, etc.

解决方案

Your question is confusing, but I assume you are saying that Hibernate is not looking for entities in the Session cache when you are performing Native queries.

SQL Query, or Native Query, is a query which Hibernate just relays to the database. Hibernate won't parse the query, and it won't parse the results. Hibernate will, though, let you handle the results, to convert the columns into properties of a class. That said, it sounds quite natural that Native Queries would bypass the Session cache. That's because Hibernate doesn't knows anything about your query, nor about the "results" of this query (which are not objects at that point).

这篇关于Hibernate SQLQuery绕过hibernate会话缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆