Java Spring引导-使用额外列对多对多关系进行分页
当使用多对多关系和一个额外的列时,我找不到一种干净简单的方法来进行分页 我的模型是这样的: 我有一个用户和一个产品模型。每个用户可以消费n种产品。每个消费都将存储在一个额外的表中,因为我想存储额外的信息,如日期等。我已经实现了如下模型,它可以正常工作,但我想将用户的消费作为可分页的,而不是检索整个集合。实现这一目标的最佳方式是什么Java Spring引导-使用额外列对多对多关系进行分页,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,当使用多对多关系和一个额外的列时,我找不到一种干净简单的方法来进行分页 我的模型是这样的: 我有一个用户和一个产品模型。每个用户可以消费n种产品。每个消费都将存储在一个额外的表中,因为我想存储额外的信息,如日期等。我已经实现了如下模型,它可以正常工作,但我想将用户的消费作为可分页的,而不是检索整个集合。实现这一目标的最佳方式是什么 @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.A
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(
mappedBy = "user",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Consumption> consumptionList = new ArrayList<>(); // never set this attribute
public List<Consumption> getConsumptionList() {
return consumptionList;
}
public void addConsumption(Product product) {
Consumption consumption = new Consumption(this, product);
consumptionList.add(consumption);
product.getConsumptionList().add(consumption);
}
public void removeConsumption(Consumption consumption) {
consumption.getProduct().getConsumptionList().remove(consumption);
consumptionList.remove(consumption);
consumption.setUser(null);
consumption.setProduct(null);
}
}
这是我的复合主键
@Embeddable
public class UserProductId implements Serializable {
@Column(name = "user_id")
private Long userId;
@Column(name = "product_id")
private Long productId;
private UserProductId() {
}
public UserProductId(Long userId, Long productId) {
this.userId = userId;
this.productId = productId;
}
}
我希望能够调用一个方法,比如“getConsumptionList(Page)”然后返回一个Pageable
我希望你能帮助我
提前谢谢你 多亏@mtshaikh idea: 只需实现分页服务:
public Page<Consumption> getConsumptionListPaginated(Pageable pageable) {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Consumption> list;
if (consumptionList.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, consumptionList.size());
list = consumptionList.subList(startItem, toIndex);
}
return new PageImpl<>(list, PageRequest.of(currentPage, pageSize), consumptionList.size());
}
公共页面getConsumptionListPaginated(可分页){
int pageSize=pageable.getPageSize();
int currentPage=pageable.getPageNumber();
int startItem=当前页面*页面大小;
名单;
if(consumptionList.size()
如果使用Spring Boot,您可以使用存储库:
@Repository
public interface ConsumptionRepo extends JpaRepository<Consumption, Long>{
List<Consumption> findByUser(User user, Pageable pageable);
}
getConsumptionList()
方法不应该包含一个Pageable
对象作为参数,并返回一个Page
,就像在public Page getConsumptionList(Pageable Pageable)
中一样吗?那么我必须自己在模型类中实现分页,对吗?不可能只在pagingandsorting repo中实现一个接口方法,但它仍然列出了所有&然后发送子列表。我们需要阻止他们打电话给alll
public Page<Consumption> getConsumptionListPaginated(Pageable pageable) {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Consumption> list;
if (consumptionList.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, consumptionList.size());
list = consumptionList.subList(startItem, toIndex);
}
return new PageImpl<>(list, PageRequest.of(currentPage, pageSize), consumptionList.size());
}
@Repository
public interface ConsumptionRepo extends JpaRepository<Consumption, Long>{
List<Consumption> findByUser(User user, Pageable pageable);
}
ConsumptionRepo.findByUser(user, PageRequest.of(page, size);