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

时间:2017-08-17 14:47:21

标签: java spring jpa spring-data spring-data-jpa

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

目前我使用Spring Data JPA的规范功能在单个实体上执行此操作:repository.findAll(specification, pageRequest)

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

以下是我的情景:

@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;
}

有没有办法使用PageRequestSpecification customerNumberitemNumbervalidDate同时用于过滤,排序和分页?

2 个答案:

答案 0 :(得分:7)

尝试这样的事情:

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。然后查询方法变得更加容易:

@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的{​​{1}}方法:

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

答案 1 :(得分:0)

我发现您可以使用可以使用自定义查询创建的连接别名,按OneToMany列的内容进行排序。在我的代码库中,它是:

@Query("select p from Person p join p.roles r")
Page<Person> search(Pageable pageable);

所以我能够使用:

r.role

通过控制器使用:

@PageableDefault(size = 15, sort = "r.role") Pageable pageable