Spring Data JPA:使用连接表进行排序和分页

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Data JPA:使用连接表进行排序和分页相关的知识,希望对你有一定的参考价值。

我有一个场景,我想对3个表参与的结果进行过滤,排序和分页。

目前我使用Spring Data JPA规范功能在单个实体上执行:qazxsw poi。

这很好用,但现在我有另一个场景,其中sort / filter属性分布在3个表中,这些表通过一对多关系连接。

这是我的场景:

repository.findAll(specification, pageRequest)

有没有办法使用@Entity public class CustomerEntity ... { ... @Column(nullable = false) public String customerNumber; @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true) public List<CustomerItemEntity> items; } @Entity public class CustomerItemEntity ... { ... @Column(nullable = false) public String itemNumber; @ManyToOne(optional = false) @JoinColumn(name = "customerId") public CustomerEntity customer; @OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true) public List<DocumentEntity> documents; } @Entity public class DocumentEntity ... { ... @Column(nullable = false) public LocalDate validDate; @ManyToOne(optional = false) @JoinColumn(name = "itemId") public CustomerItemEntity item; } PageRequest SpecificationcustomerNumberitemNumber同时用于过滤,排序和分页?

答案

尝试这样的事情:

validDate

但我不知道为什么你需要Specification<CustomerEntity> joins = (customer, query, cb) -> { // from CustomerEntity c // join c.items i Join<CustomerEntity, CustomerItemEntity> items = customer.join("items"); // join i.documents d Join<CustomerItemEntity, DocumentEntity> documents = items.join("documents"); // // where c.customerNumber = ?1 and i.itemNumber = ?2 and d.validDate = ?3 return cb.and( customer.equal(customer.get("customerNumber", customerNumber)), items.equal(items.get("itemNumber", itemNumber)), documents.equal(documents.get("validDate", validDate)) ); }; // sort by c.customerNumber asc PageRequest pageRequest = new PageRequest(0, 2, new Sort(Sort.Direction.ASC, "customerNumber")); Page<CustomerEntity> customerPage = CustomerRepo.findAll(joins, pageRequest);

你可以使同样更简单:

Specification

但是这一切都没有意义,因为你的三个实体具有顺序的一对多关联。在这种情况下,您可以只使用最后一个条件而不是三个条件:@Query("select c from CustomerEntity c join c.items i join i.documents d where c.customerNumber = ?1 and i.itemNumber = ?2 and d.validDate = ?3") Page<CustomerEntity> getCustomers(String customerNumber, String itemNumber, LocaleDate validDate, Pageable pageable); 。然后查询方法变得更加容易:

where d.validDate = ?1

UPDATE

要通过连接实体的字段添加排序,我们可以使用@Query("select c from CustomerEntity c join c.items i join i.documents d where d.validDate = ?1") Page<CustomerEntity> getCustomers(LocaleDate validDate, Pageable pageable); orderBy方法:

query

要按几个参数排序,您可以将它们传递给逗号或Specification<CustomerEntity> joins = (customer, query, cb) -> { Join<CustomerEntity, CustomerItemEntity> items = customer.join("items"); Join<CustomerItemEntity, DocumentEntity> documents = items.join("documents"); // Ascending order by 'Document.itemNumber' query.orderBy(cb.asc(documents.get("itemNumber"))); return cb.and( customer.equal(customer.get("customerNumber", customerNumber)), items.equal(items.get("itemNumber", itemNumber)), documents.equal(documents.get("validDate", validDate)) ); }; Page<CustomerEntity> customerPage = CustomerRepo.findAll(joins, new PageRequest(0, 2)); 分隔的方法:

List

以上是关于Spring Data JPA:使用连接表进行排序和分页的主要内容,如果未能解决你的问题,请参考以下文章

spring-data-jpa的简单使用动态sql分页排序

Spring Data 系列学习Spring Data JPA 自定义查询,分页,排序,条件查询

Spring Data JPA 规范 - 连接中的不同+按列排序

Spring DATA JPA中的关于多表连接的问题

Spring Data JPA 查询从一个连接表中选择所有值

使用Spring Data JPA进行数据分页与排序