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

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

问题描述

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

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".

我发现了一个 explanation 似乎解释了它,但所有示例实际上都是在代码中获取和管理事务.我们在整个类上使用 @TransactionAttribute 注释来更改此代码,并找到了许多发生这种行为的地方,但我并不完全相信该解释适用于简单地包含在注释中的代码——我假设任何使用休眠管理器的东西都将依赖于会话中的对象缓存.

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.

如果我用不正确的术语等引用了 hibernate 中的概念,请提前道歉.

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

推荐答案

您的问题令人困惑,但我假设您是说 Hibernate 在您执行 Native 查询时不会在会话缓存中查找实体.

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 查询或本地查询是 Hibernate 仅中继到数据库的查询.Hibernate 不会解析查询,也不会解析结果.但是,Hibernate 会让您处理结果,将列转换为类的属性.也就是说,本机查询绕过会话缓存听起来很自然.那是因为 Hibernate 对您的查询一无所知,也不了解此查询的结果"(此时它们不是对象).

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 绕过休眠会话缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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