HQL查询是否总是命中数据库并得到结果?

发布于 2024-12-28 17:12:11 字数 512 浏览 1 评论 0原文

我正在经历 Hibernate 以及何时使用 Criteria 与 HQL 的情况,我的理解是,使用 Hibernate,每次我们通过 CriteriaHQL< 查询数据库时/code> 在这两种情况下,hibernate 都会获取结果集并放入内存中,然后当我们再次调用该查询时,将从内存中获取数据而不是访问该数据库,我的理解正确吗?

另外,正如您从下面提到的问题的评论中看到的那样,建议 Hibernate Criteria 将从会话中获取数据,并且 HQL 将始终去并命中数据库,因此对 HQL 查询的任意数量的多次调用都将去并命中数据库,如果这是那么 HQL 造成的问题比解决的问题还要多。

请就此提出建议,因为我对情况有点困惑。

参考问题

I was going through hibernate and situations when to use Criteria vs HQL and my understanding is that with Hibernate, everytime when we are querying database either by Criteria or HQL in both instances hibernate would get result set and put in memory and then when we call that query again, data would be fetched from memory rather then hitting that database, is my understanding correct?

Also as you can see from comments to question mentioned below, it was suggested that Hibernate Criteria would get data from session and HQL would always go and hit database and so any number of multiple calls to HQL query will go and hit database and if this is the case then HQL causes more problems than solving.

Kindly advise on this as am little bit confused with the situation.

Reference to question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2025-01-04 17:12:11

这取决于您进行的查询类型以及缓存设置。

Hibernate 有三种缓存:会话缓存、查询缓存和二级缓存。会话缓存始终打开,但其他两个可以禁用。

通常,缓存并不是优先使用 Criteria API 而不是 HQL 的原因,反之亦然。它们大多只是本质上相同的东西的不同接口。

请参阅http://www.javalobby.org/java/forums/t48846.htmlhttp://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

It depends on what kind of queries you are making and about your cache settings.

Hibernate has three kind of caches: session cache, query cache and 2nd level cache. Session cache is always on but the other two can be disabled.

Usually the caching is not the reason to favor Criteria API over HQL or vice versa. They are mostly just different interfaces for essentially the same thing.

See http://www.javalobby.org/java/forums/t48846.html and http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html

等风也等你 2025-01-04 17:12:11

基本上,如果您要生成查询,您可能会访问数据库,但如果您已缓存查询和参数,则例外。

如果您使用 @Id 获取实体,Hibernate 查询(无论您使用 Criteria 还是 HQL)只会从会话缓存(一级缓存)返回实体。

为了缓存查询,您可以使用以下语法:

session.createQuery("from X as x").setCacheable(true);

编辑注释:

查询与使用 @Id 的 get 不同。要通过 @Id 获取对象,您可以编写如下内容:

Entity myEntity = sessionFactory.getCurrentSession().get(Entity.class, 1);

Basically if you're generating queries you're probably going to hit the database, the exception to this is if you've cached the query and parameters.

Hibernate queries (whether you use Criteria or HQL) will only return entities from the session cache (first level cache) if you get it with the @Id.

In order to cache a query you can use the following syntax:

session.createQuery("from X as x").setCacheable(true);

Edited for comments:

A query is not the same as a get with @Id. To get an object by its @Id you would write something like:

Entity myEntity = sessionFactory.getCurrentSession().get(Entity.class, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文