Hibernate:如何进行 EXISTS 查询? (不是子查询)
例如:
EXISTS ( SELECT * FROM [table] WHERE ... )
如何使用 Hibernate 进行这样的查询?
For example:
EXISTS ( SELECT * FROM [table] WHERE ... )
How to make such query using Hibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
HQL 不允许使用
exists
语句。UPD 从 Hibernate 5 开始 它支持将其作为 WHERE 中的谓词
您可以使用多种方法:
boolean contains = session.createQuery("select 1 from PersistentEntity where isn't (从 PersistentEntity p 中选择 1,其中 ...)").uniqueResult() != null;
。感谢以下作者。boolean contains = session.createQuery("from PersistentEntity where ...").setMaxResults( 1).uniqueResult() != null;
但这将强制 Hibernate 加载所有字段并对对象进行水合作用,即使您只需要检查 null 也是如此。session.get(PersistentEntity.class, id) != null
如果您启用二级缓存,这会工作得更快,但如果您需要更多的条件而不仅仅是id<,这将是一个问题/代码>。
getSession().createQuery("select 1 from PersistentEntity where ...").uniqueResult() != null)
HQL doesn't allow to use
exists
statement.UPD Starting from Hibernate 5 it supports it as a predicate in WHERE
You can use several approaches:
boolean exists = session.createQuery("select 1 from PersistentEntity where exists (select 1 from PersistentEntity p where ...)").uniqueResult() != null;
. Thank to author bellow.count(*) > 0
but this is bad for performance Avoid Using COUNT() in SQL When You Could Use EXISTS()boolean exists = session.createQuery("from PersistentEntity where ...").setMaxResults(1).uniqueResult() != null;
but this will force Hibernate to load all fields and make hydration to object even if you need just to check for a null.session.get(PersistentEntity.class, id) != null
and this will work faster if you enabled second level cache but it will be a problem if you need more criteries than justid
.getSession().createQuery("select 1 from PersistentEntity where ...").uniqueResult() != null)
如果您的目标是检查某些空集,您可以使用简单的 HQL 查询:
If your goal is inspect some set on emptiness, you may use simple HQL query:
如果我们使用WHERE子句,数据库可能必须扫描整个表来统计符合我们条件的记录,但我们可以限制只搜索一条记录,这足以说明空了。
如果没有任何搜索过滤器,则先前的查询将被接受,因为数据库将使用索引进行一些优化。
所以我想与前一个查询相比,以下查询会提高一些性能:
If we are using WHERE clause, the database may have to scan the whole table to count records that matches our criteria, but we can limit to search only for one record, it is enough to say about emptiness.
If there wasn't any search filters the previous query would be admissible as the database would do some optimisation with the use of an index.
so I suppose following query will increase some perfomance comparing to previous one:
有一些关于使用 EXISTS 的很好的评论,但没有示例。这是我在 Hibernate 5.2 中的解决方案:
There's some nice comments about using EXISTS but no examples. Here is my solution in Hibernate 5.2:
另外,如果您使用 JpaRepository,您可以通过 ORM 轻松检查实体是否存在。这是一个 Kotlin 示例:
Also, if you use
JpaRepository
, you can easily check entity existence via ORM. Here is a Kotlin example:如果您使用
JpaRepository
,如果存在标准很简单,例如:,那么这可能会很简单:
如果不是,则此解决方案可能就是您要寻找的解决方案。
编写一个如下所示的查询:
示例:
测试您的查询,然后将其添加到您的存储库中
If you use
JpaRepository
, this could be simple if the existence criteria is simple like:If not, this solution may be the one you look for.
Write a query that looks like:
Example:
test your query, then add it into your repo
我使用了以下内容:
SELECT COUNT(e) FROM Entity e
。在 Spring Data JPA 中工作得非常好。I used the following:
SELECT COUNT(e) FROM Entity e
. Worked perfectly fine in Spring Data JPA.尝试:
与 Spring Data 配合良好。
Try:
Works fine with Spring Data.