解决 JPA 查询查找连接列表中的最后一个条目

发布于 2024-07-11 10:26:03 字数 977 浏览 5 评论 0原文

给出了以下类结构:

class Job 
{
    String description;
    Collection<JobHistory> history;
}

class JobHistory
{
    Date assignDate;
    User jobOwner;
}

class JobOwner 
{
    String name;
    String id;
}

该类结构可通过 JPA 在数据库上访问。 在 DAO 层中,我可以使用 JPA 语法编写查询。

问题:我想要一个包含给定所有者的 JobJobHistory 条目的列表,该所有者具有给定的 ID,并且谁是 Jobhistory 中的最后一个作业(按分配日期排序)。 听起来很复杂,也许更简单:给我所有作业和 JobHistory,其中指定所有者是作业的实际所有者。

更新:为了清楚起见,我将稍微更改类的名称。

class Job 
{
    String description;
    Collection<JobOwnerHistory> history;
}

class JobOwnerHistory
{
    Date assignDate;
    User jobOwner;
}

class JobOwner 
{
    String name;
    String id;
}

每个作业都有其所有者的历史记录,按分配日期排序。 实际所有者获得了上次分配的作业(即MAX(assignDate))。 我想为每个作业找到特定用户 UserJobOwnerHistory 条目以及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵山崖 2024-07-18 10:26:03

我找到了以下查询答案:

SELECT j, h FROM Job j JOIN j.history h JOIN h.jobOwner u
WHERE u.name = :name AND 
    (SELECT MAX(h2.assignDate) FROM Job j2 JOIN j2.history h2
     WHERE h2 member of j.history) = h.assignDate

查询中最重要的部分是使用 MAX(h2.assignDate) 进行子选择,因为我想获取作业和最新的所有者历史记录中的条目。

I found the following answer for the query:

SELECT j, h FROM Job j JOIN j.history h JOIN h.jobOwner u
WHERE u.name = :name AND 
    (SELECT MAX(h2.assignDate) FROM Job j2 JOIN j2.history h2
     WHERE h2 member of j.history) = h.assignDate

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.

长亭外,古道边 2024-07-18 10:26:03

尝试:

SELECT j, j.history FROM Job j JOIN User u WHERE u.name = :name

如果我要在 EclipseLink 中执行此操作,我会稍微更改它:

public List<Job> getAllJobsForUser(String username) {
  List<Job> jobs = entityManager
    .createQuery("SELECT j FROM Job j JOIN User u WHERE u.name = :name")
    .setParameter("name", username)
    .setHint(QueryHints.BATCH, "j.history")
    .queryForList();
}

区别是什么? 在第一个版本中,您返回两个对象,因此您必须从列表或对象数组中检索它们,而在第二个版本中,查询提示仅从(假设的)惰性一对多关系加载所有作业历史记录。

我不知道 Hibernate 是否有与此等效的功能。 Toplink Essentials 没有。 但这是我最喜欢的 EclipseLink 功能之一。

哦,显然您可以(并且可能应该)使用命名查询而不是像我所做的那样即席查询(因为可以在构建期间验证这些查询)。

Try:

SELECT j, j.history FROM Job j JOIN User u WHERE u.name = :name

If I were to do this in EclipseLink, I would change it slightly:

public List<Job> getAllJobsForUser(String username) {
  List<Job> jobs = entityManager
    .createQuery("SELECT j FROM Job j JOIN User u WHERE u.name = :name")
    .setParameter("name", username)
    .setHint(QueryHints.BATCH, "j.history")
    .queryForList();
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文