解决 JPA 查询查找连接列表中的最后一个条目
给出了以下类结构:
class Job
{
String description;
Collection<JobHistory> history;
}
class JobHistory
{
Date assignDate;
User jobOwner;
}
class JobOwner
{
String name;
String id;
}
该类结构可通过 JPA 在数据库上访问。 在 DAO 层中,我可以使用 JPA 语法编写查询。
问题:我想要一个包含给定所有者的 Job
和 JobHistory
条目的列表,该所有者具有给定的 ID,并且谁是 Jobhistory
中的最后一个作业(按分配日期排序)。 听起来很复杂,也许更简单:给我所有作业和 JobHistory,其中指定所有者是作业的实际所有者。
更新:为了清楚起见,我将稍微更改类的名称。
class Job
{
String description;
Collection<JobOwnerHistory> history;
}
class JobOwnerHistory
{
Date assignDate;
User jobOwner;
}
class JobOwner
{
String name;
String id;
}
每个作业
都有其所有者的历史记录,按分配日期
排序。 实际所有者获得了上次分配的作业(即MAX(assignDate)
)。 我想为每个作业找到特定用户 User
的 JobOwnerHistory
条目以及 MAX(assignDate)
。
Following class structure is given:
class Job
{
String description;
Collection<JobHistory> history;
}
class JobHistory
{
Date assignDate;
User jobOwner;
}
class JobOwner
{
String name;
String id;
}
This class-structure is accessible on the db via JPA. In the DAO-Layer I can write queries in JPA syntax.
The Problem: I want a list with Job
and JobHistory
entries for a given owner with given id and who is the last one in the Jobhistory
of the job (ordered by assignDate). Sounds quite complicated, perhaps simpler: give me all jobs and JobHistory
where specified owner is the actual owner of the job.
Update: for clarity I will slightly change the names of the classes.
class Job
{
String description;
Collection<JobOwnerHistory> history;
}
class JobOwnerHistory
{
Date assignDate;
User jobOwner;
}
class JobOwner
{
String name;
String id;
}
Every Job
has a history of his owners sorted by assignDate
. The actual owner got the job last assigned (i.e. MAX(assignDate)
). I want find for every job the JobOwnerHistory
entry with MAX(assignDate)
for a specific user User
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了以下查询答案:
查询中最重要的部分是使用
MAX(h2.assignDate)
进行子选择,因为我想获取作业和最新的所有者历史记录中的条目。I found the following answer for the query:
The most important part in the query is the subselect with
MAX(h2.assignDate)
because I want to get the job and the newest entry in the owner-history.尝试:
如果我要在 EclipseLink 中执行此操作,我会稍微更改它:
区别是什么? 在第一个版本中,您返回两个对象,因此您必须从列表或对象数组中检索它们,而在第二个版本中,查询提示仅从(假设的)惰性一对多关系加载所有作业历史记录。
我不知道 Hibernate 是否有与此等效的功能。 Toplink Essentials 没有。 但这是我最喜欢的 EclipseLink 功能之一。
哦,显然您可以(并且可能应该)使用命名查询而不是像我所做的那样即席查询(因为可以在构建期间验证这些查询)。
Try:
If I were to do this in EclipseLink, I would change it slightly:
The difference? In the first version, you're returning two objects, so you have to retrieve them from a List or Object arrays whereas in the second, the query hint just loads all the job histories from an (assumedly) lazyy one-to-many relationship.
I don't know if Hibernate has an equivalent to this. Toplink Essentials doesn't. But it's one of my favourite features of EclipseLink.
Oh and obviously you can (and probably should) use a named query instead of an adhoc query like I've done (since those can be verified during the build).