第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

春靴 - 使用額外列進行多對多關(guān)系的分頁

春靴 - 使用額外列進行多對多關(guān)系的分頁

瀟瀟雨雨 2022-08-17 15:23:46
在對多對多關(guān)系使用額外列時,我找不到一種干凈簡單的方法來進行分頁。我的模型看起來像這樣:我有一個用戶和一個產(chǎn)品模型。每個用戶可以消費n種產(chǎn)品。每個消耗將存儲在一個額外的表中,因為我想存儲額外的信息,如日期等。我已經(jīng)實現(xiàn)了如下模型并且它的工作原理,但我想將用戶的消費作為可尋呼的,而不是檢索整個集合。實現(xiàn)它的最佳方法是什么?@Entitypublic 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;    }}這是我存儲消耗的類。@Entitypublic class Consumption {    @EmbeddedId    private UserProductId id;    @ManyToOne(fetch = FetchType.LAZY)    @MapsId("userId")    private User user;}我希望能夠調(diào)用一個方法,例如“getConsumptionList(Page page)”,然后返回一個Pageable。我希望你能幫助我!
查看完整描述

2 回答

?
ibeautiful

TA貢獻1993條經(jīng)驗 獲得超6個贊

好吧,如果使用Spring Boot,你可以使用存儲庫:


@Repository

public interface ConsumptionRepo extends JpaRepository<Consumption, Long>{

    List<Consumption> findByUser(User user, Pageable pageable);

}

然后你可以簡單地調(diào)用它


ConsumptionRepo.findByUser(user, PageRequest.of(page, size);


查看完整回答
反對 回復(fù) 2022-08-17
?
MMTTMM

TA貢獻1869條經(jīng)驗 獲得超4個贊

由于@mtshaikh想法,我終于為我的問題找到了解決方案:


只需實現(xiàn)一個分頁服務(wù):


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());


    }


查看完整回答
反對 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號