休眠子查询 [英] Hibernate subquery
问题描述
在使用Hibernate创建子查询时遇到问题.不幸的是Subqueries类几乎完全没有文档,因此我完全不知道如何将以下SQL转换为Hibernate Criteria:
I have a problem in creating subqueries with Hibernate. Unfortunately the Subqueries class is almost entirely undocumented, so I have absolutely no clue how to convert the following SQL into a Hibernate Criteria:
SELECT id
FROM car_parts
WHERE car_id IN ( SELECT id FROM cars WHERE owner_id = 123 )
我希望以下内容能够正常工作":
I was hoping the following would 'just work':
session.createCriteria(CarParts.class).add(eq("car.owner", myCarOwner));
但不幸的是,事实并非如此.因此,看来我实际上必须使用Subqueries类来创建Criteria.但是我无法通过Google找到一个合理的例子,因此我不得不在这里提出要求.
but unfortunately it does not. So it seems I actually have to use the Subqueries class to create the Criteria. But I was unable to find a reasonable example though Google, so that leads me to asking it here.
推荐答案
在添加eq表达式之前,尝试为"car"属性创建别名:
Try to create an alias for the "car" property before adding the eq expression like this:
session.createCriteria(CarParts.class)
.createAlias("car", "c")
.add(eq("c.owner", myCarOwner));
这篇关于休眠子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!