java Hibernate Criteria 连接到包含外键的表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15893044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Criteria join to table containing foreign key
提问by John Turnbull
I have two tables:
我有两个表:
Client (clientId, firstName, lastName, gender)
Event (clientId, eventId)
I need to represent a query similar to the following using Criteria:
我需要使用 Criteria 表示类似于以下内容的查询:
SELECT c.clientId, c.firstName, c.lastName, c.gender, MAX(eventId)
FROM Client c JOIN Event e ON c.clientId = e.clientId
GROUP BY c.clientId, c.firstName, c.lastName, c.gender
I have tried this:
我试过这个:
final Criteria criteria = session.createCriteria(Client.class);
criteria.setFetchMode("Event", FetchMode.JOIN);
criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("clientId")).add(Projections.max("eventId")));
but it throws an exception on the last line with the message:
但它在消息的最后一行抛出异常:
HibernateQueryException: could not resolve property: eventId of: Client
HibernateQueryException: 无法解析属性: eventId of: Client
How can I specify the join between the Client
table which itself contains no column related to the Event table but the clientId
column on the Event
table is a foreign key back into the Client
table?
如何指定Client
本身不包含与事件表相关的clientId
列但Event
表上的列是返回表的外键的Client
表之间的连接?
As you can see, it's really driven off the Client
table and that I only need to select the maximum eventId
from the Event
table. Also, as I mentioned, I am trying to make a change to an existing Criteria query which is based on the Client
class. It is used to retrieve all the columns for all active clients. I just need to add one extra column to the query results - the maximum eventId
.
正如您所看到的,它实际上是从Client
表中删除的,我只需eventId
要从Event
表中选择最大值。此外,正如我所提到的,我正在尝试对基于Client
类的现有 Criteria 查询进行更改。它用于检索所有活动客户端的所有列。我只需要向查询结果中添加一个额外的列 - 最大值eventId
。
回答by Mohan Raj B
Use alias
使用别名
Criteria criteria = session.createCriteria(Event.class, "et").
createAlias("et.Client", "ct").
setProjection(Projections.projectionList().
add(Projections.groupProperty("et.clientId")).
add(Projections.max("et.eventId")));
For more details on criteria, refer Criteria Queries
有关条件的更多详细信息,请参阅条件查询
回答by Matin Kh
That is obvious. Because Client
class does not have eventId
property, and your criteria is defined for Client
class.
这是显而易见的。因为Client
class 没有eventId
属性,并且您的标准是为Client
class定义的。
When trying to use a property of B class inside a Criteria for A, you have to use Aliases.
当尝试在 A 的标准中使用 B 类的属性时,您必须使用别名。
All you have to do is to modify your code like this:
你所要做的就是像这样修改你的代码:
final Criteria criteria = session.createCriteria(Event.class, "event");
criteria.createAlias("event.client", "client");
criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("clientId")).add(Projections.max("eventId")));
UPDATED更新(根据您的评论)
As your query needs Event
class, you have to have a Criteria
for this class. So you have to something like this:
由于您的查询需要Event
类,因此您必须Criteria
为此类提供一个。所以你必须是这样的:
final Criteria criteria = session.createCriteria(Event.class, "event");
criteria.createAlias("event.client", "client");
//The criteria below, is returning clientId
DetachedCriteria eventCr = DetachedCriteria.forClass(Event.class, "event");
eventCr.setProjection(Projections.projectionList().add(Projections.groupProperty("clientId")).add(Projections.max("eventId")));
//Now using subqueries you can achieve your goal
criteria.add(Subqueries.propertyIn("clientId", eventCr));
I don't know for sure what you're looking for, but I hope I have given you some good hints. You might want to try Subqueries.propertyEq
instead if your query must return a single id.
我不确定你在找什么,但我希望我给了你一些好的提示。Subqueries.propertyEq
如果您的查询必须返回单个 ID,您可能想尝试一下。