Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据休眠:N+;1问题和分页_Spring_Hibernate_Jpa_Pagination - Fatal编程技术网

Spring数据休眠:N+;1问题和分页

Spring数据休眠:N+;1问题和分页,spring,hibernate,jpa,pagination,Spring,Hibernate,Jpa,Pagination,TL;DR--我找不到一种方法在同时进行分页时解决N+1问题 我的实体: @Entity public class Invoice { @Id @JsonView(InvoiceView.ShortView.class) @GeneratedValue(strategy = GenerationType.AUTO) private UUID id; // other stuff @OneToMany(targetEntity = InvoiceIte

TL;DR--我找不到一种方法在同时进行分页时解决N+1问题

我的实体:

@Entity
public class Invoice {

    @Id
    @JsonView(InvoiceView.ShortView.class)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

// other stuff

    @OneToMany(targetEntity = InvoiceItem.class)
    @JoinColumn(name = "invoice")
    private List<InvoiceItem> items;
}

@Entity
public class InvoiceItem {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

// other stuff
}


他们就是这么做的:把所有的东西都拿来,然后把要求的页面还给他们。当然,每次请求新页面时

Vlad Mihalcea在本文中描述了您的问题以及如何解决它


基本上,您需要编写一个本机查询。

这不是一个解决方案。这只是证实了这是不可能的。这是正确的链接(在我链接的文章的介绍中)。您需要编写一个本地QueryTanks mate——这个问题有很多解决方法,不涉及使用框架(或涉及使用另一个:)——我只是想看看JPA的方法是否真的可用。显然不是。
    @EntityGraph(attributePaths = {"vendor","items"})
    @Query(value = "select i from Invoice i where i.status=:status")
    Page<Invoice> getInvoicesWithItemsByStatus(@Param("status") Status status, Pageable pageSpec);
        @Query(value = "select i from Invoice i join fetch i.items join fetch i.vendor where i.status=:status",
            countQuery = "select count(i) from Invoice i where i.status=:status")
    Page<Invoice> getInvoicesWithItemsByStatus(@Param("status") Status status, Pageable pageSpec);
HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!