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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:11:44  来源:igfitidea点击:

Hibernate Criteria join to table containing foreign key

javasqlhibernatehibernate-criteria

提问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 Clienttable which itself contains no column related to the Event table but the clientIdcolumn on the Eventtable is a foreign key back into the Clienttable?

如何指定Client本身不包含与事件表相关的clientId列但Event表上的列是返回表的外键的Client表之间的连接?

As you can see, it's really driven off the Clienttable and that I only need to select the maximum eventIdfrom the Eventtable. Also, as I mentioned, I am trying to make a change to an existing Criteria query which is based on the Clientclass. 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 Clientclass does not have eventIdproperty, and your criteria is defined for Clientclass.

这是显而易见的。因为Clientclass 没有eventId属性,并且您的标准是为Clientclass定义的。

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 Eventclass, you have to have a Criteriafor 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.propertyEqinstead if your query must return a single id.

我不确定你在找什么,但我希望我给了你一些好的提示。Subqueries.propertyEq如果您的查询必须返回单个 ID,您可能想尝试一下。