简体   繁体   English

Spring Data JPA按嵌套对象集合大小排序

[英]Spring data jpa sort by nested object collection size

I am using a spring boot application and there exists the following 2 entities. 我正在使用Spring Boot应用程序,并且存在以下2个实体。

Person.java Person.java

@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "PERSON")
public class Person extends BaseEntity {
    @NotNull
    @Enumerated(EnumType.STRING)
    private StatusType status;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "PERSON_ADDRESS",
            joinColumns = @JoinColumn(
                    name = "person_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(
                    name = "address_id", referencedColumnName = "id"))
    private Collection<Info> addresses;
}

Address.java Address.java

@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "ADDRESS")
public class Address extends BaseEntity {
    @NotNull
    private String address;

    @ManyToMany(mappedBy = "addresses")
    private Collection<Person> persons;
}

I am using a JpaRepository as follows. 我正在使用JpaRepository如下。

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
}

And I use the following statement to implement the pagination and sorting. 我使用以下语句实现分页和排序。

Page<Person> allPersons = personRepository.findAll(pageable);

where pageable is an instance of org.springframework.data.domain.Pageable . 其中pageableorg.springframework.data.domain.Pageable的实例。

I am able to sort using the other columns in Person . 我可以使用Person的其他列进行排序。 But I want to sort based on the addresses collection, based on the number of Address record for each Person entity. 但是我想基于地址集合,基于每个Person实体的Address记录数进行排序。

In short, I wanted to sort the Persons based on the collection size of Collection . 简而言之,我想根据Collection的集合大小对 Persons进行排序。 The sorting order (ASC or DESC) comes from the front end. 排序顺序(ASC或DESC)来自前端。

Any idea how the Pageable object should look like to implement this? 知道Pageable对象应如何实现吗? Also without returning any duplicate Person record if more than one Address exist for a Person . 如果一个Person存在多个Address则也不会返回任何重复的Person记录。

Here is the solution according to Order by count using Spring Data JpaRepository . 这是使用Spring Data JpaRepository根据按订单排序的解决方案。

In my experiment, I tried to define Repository as follows, but there is problem with that; 在我的实验中,我尝试如下定义存储库,但是这样做有问题。 as we are defining the query on PersonRepository for sorting based on addressCount spring data looks into Person. 当我们在PersonRepository上定义查询以基于addressCount进行排序时,spring数据将查找Person。

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {

    @Query(
        value = "select p from Person p join p.addresses ad group by p",
        countQuery = "select count(p) from Person p"
    )
    Page <Person> findAllWithAddressCount(Pageable pageable);
}

So as a workaround I tried to shift the sorting logic into query definition itself and I end up with 2 versions for ASC and DESC mode: 因此,作为一种解决方法,我尝试将排序逻辑转换为查询定义本身,最终得到了两个版本的ASC和DESC模式:

@Repository
public interface PersonRepository extends JpaRepository <Person, Long> {

    @Query(
        value = "select p from Person p join p.addresses ad group by p Order By addressCount asc",
        countQuery = "select count(p) from Person p"
    )
    Page<Person> findAllOrderByAddressCountAsc(Pageable pageable);

    @Query(
        value = "select p from Person p join p.addresses ad group by p Order By addressCount desc",
        countQuery = "select count(p) from Person p"
    )
    Page<Person> findAllOrderByAddressCountDesc(Pageable pageable);
}

Hope this could help. 希望这会有所帮助。

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

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