[英]Hibernate Criteria join to table containing foreign key
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:客户端
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. 如您所见,它实际上是从
Client
表中eventId
,我只需要从Event
表中选择最大eventId
。 Also, as I mentioned, I am trying to make a change to an existing Criteria query which is based on the Client
class. 另外,正如我提到的,我试图对基于
Client
类的现有Criteria查询进行更改。 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
. 我只需要在查询结果中增加一列-最大
eventId
。
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 有关条件的更多详细信息,请参阅条件查询
That is obvious. 那是显而易见的。 Because
Client
class does not have eventId
property, and your criteria is defined for Client
class. 因为
Client
类没有eventId
属性,并且您的条件是为Client
类定义的。
When trying to use a property of B class inside a Criteria for A, you have to use Aliases. 尝试在A的Criteria中使用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")));
As your query needs Event
class, you have to have a Criteria
for this class. 由于查询需要
Event
类,因此您必须具有该类的Criteria
。 So you have to something like this: 因此,您必须执行以下操作:
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. 如果查询必须返回单个ID,则可能需要尝试
Subqueries.propertyEq
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.