简体   繁体   English

使用 Spring Data JPA 通过 @OneToMany 关系属性对实体进行排序

[英]Sorting entities by their @OneToMany relationship property with Spring Data JPA

I'm working on Spring Boot web application which uses Spring Data JPA for its persistance layer.我正在开发使用 Spring Data JPA 作为其持久层的 Spring Boot Web 应用程序。 When retrieving entities from repository I'm using Spring's Data JPA Sort object to sort them.从存储库检索实体时,我使用 Spring 的 Data JPA Sort对象对它们进行排序。 It works when I'm sorting by retrieved entity property or by it's @OneToOne relationship object property, but I would like to use it to sort by one of the @OneToMany relationship object properties.当我按检索到的实体属性或 @OneToOne 关系对象属性进行排序时,它会起作用,但我想用它来按 @OneToMany 关系对象属性之一进行排序。

Let's explain with an example: suppose I have entity object Author which has one to many relationship with another entity Book .让我们用一个例子来解释:假设我有实体对象Author ,它与另一个实体Book有一对多的关系。 My entity classes in simplest form would look like this:我的最简单形式的实体类如下所示:

@Entity
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToMany(mappedBy = "author")
    private List<Book> books;

    <constructor, getters, setters etc.>
}

and

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String title;

    @ManyToOne
    @JoinColumn(name = "author_id")
    private Author author;

    <constructor, getters, setters etc.>
}

now, when I'm retrieving authors with Spring's repository interface I'm passing Sort object to it like this one:现在,当我使用 Spring 的存储库接口检索作者时,我将 Sort 对象传递给它,如下所示:

new Sort(Sort.Direction.ASC, "id")

which gives me results sorted by author id ascending.这给了我按作者 id 升序排序的结果。 I would like to pass something like this:我想通过这样的事情:

new Sort(Sort.Direction.ASC, "books.title")

Let's say I have this data in the database (simplified table just to show example):假设我在数据库中有这些数据(简化表只是为了显示示例):

author  | book_title
---------------------
Andrew  | Letter C
Barbara | Letter A
Andrew  | Letter B
Barbara | Letter D

The resulting list would be Barbara (her book "Letter A" is first after sorting by book title) then Andrew.结果列表将是芭芭拉(她的书“字母 A”是按书名排序后的第一个)然后是安德鲁。

Passing new Sort(Sort.Direction.ASC, "books.title") right now results in "Barbara, Andrew, Andrew, Barbara" - which means there are duplicates on resulting list - I would like results to be distinct.现在传递new Sort(Sort.Direction.ASC, "books.title")导致“Barbara, Andrew, Andrew, Barbara”——这意味着结果列表中有重复项——我希望结果是不同的。

I do not want to use @OrderBy on collection in Author as I'm not interested in actual books order - only the authors.不想在 Author 的收藏中使用@OrderBy ,因为我对实际的图书订单不感兴趣 - 只有作者。

I do not want to sort results with JPQL on repository level with @Query (it would be probably possible with some JPQL subquery and virtual field maybe), as I need it to be able to accept sortable fileds dynamically (it may be title now, but isbn number on other case and my API should be able to take one or the other).不想使用@Query在存储库级别使用 JPQL 对结果进行排序(使用某些 JPQL 子查询和虚拟字段可能是可能的),因为我需要它能够动态接受可排序的文件(现在可能是标题,但其他情况下的 isbn 编号和我的 API 应该能够采用其中一种)。

It has to work with Spring Specification API which I'm using to filter results.必须与我用来过滤结果的 Spring Specification API 一起使用。

Is it possible?是否可以?

I had the same question, but I found this answer:我有同样的问题,但我找到了这个答案:

@OneToMany
@OrderBy("value ASC") // sort by value ASC
private List<PropertyDefinition> propertyDefinitions;

Check the answer in this link: Spring Data JPA sorting on nested collection检查此链接中的答案: 嵌套集合上的 Spring Data JPA 排序

It solve my issue.它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM