在对多对多关系使用额外列时,我找不到一种干净简单的方法来进行分页。
我的模型看起来像这样:
我有一个用户和一个产品模型。每个用户可以消费n种产品。每个消耗将存储在一个额外的表中,因为我想存储额外的信息,如日期等。我已经实现了如下模型并且它的工作原理,但我想将用户的消费作为可寻呼的,而不是检索整个集合。实现它的最佳方法是什么?
@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);
}
}
--
@Entity
@NaturalIdCache
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(
mappedBy = "product",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Consumption> consumptionList = new ArrayList<>();
public List<Consumption> getConsumptionList() {
return consumptionList;
}
}
这是我存储消耗的类。
@Entity
public class Consumption {
@EmbeddedId
private UserProductId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("userId")
private User user;
}
我希望能够调用一个方法,例如“getConsumptionList(Page page)”,然后返回一个Pageable。
我希望你能帮助我!
ibeautiful
MMTTMM
相关分类