春靴 - 使用额外列进行多对多关系的分页

在对多对多关系使用额外列时,我找不到一种干净简单的方法来进行分页。


我的模型看起来像这样:


我有一个用户和一个产品模型。每个用户可以消费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。


我希望你能帮助我!


潇潇雨雨
浏览 86回答 2
2回答

ibeautiful

好吧,如果使用Spring Boot,你可以使用存储库:@Repositorypublic interface ConsumptionRepo extends JpaRepository<Consumption, Long>{&nbsp; &nbsp; List<Consumption> findByUser(User user, Pageable pageable);}然后你可以简单地调用它ConsumptionRepo.findByUser(user, PageRequest.of(page, size);

MMTTMM

由于@mtshaikh想法,我终于为我的问题找到了解决方案:只需实现一个分页服务:public Page<Consumption> getConsumptionListPaginated(Pageable pageable) {&nbsp; &nbsp; &nbsp; &nbsp; int pageSize = pageable.getPageSize();&nbsp; &nbsp; &nbsp; &nbsp; int currentPage = pageable.getPageNumber();&nbsp; &nbsp; &nbsp; &nbsp; int startItem = currentPage * pageSize;&nbsp; &nbsp; &nbsp; &nbsp; List<Consumption> list;&nbsp; &nbsp; &nbsp; &nbsp; if (consumptionList.size() < startItem) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list = Collections.emptyList();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int toIndex = Math.min(startItem + pageSize, consumptionList.size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list = consumptionList.subList(startItem, toIndex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return new PageImpl<>(list, PageRequest.of(currentPage, pageSize), consumptionList.size());&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java