Spring Data JPA Pageable with named entity graph

时间:2016-09-30 13:12:58

标签: spring jpa spring-data

我正在使用带有 JPA 2.1 的Spring数据来检索具有分页和放大器的实体。使用以下方法的规范(标准构建者):

final static

如何在此方法上指定Page<T> findAll(Specification<T> spec, Pageable pageable) ?是的,如果我用@EntityGraph注释,Spring会停止分页并一次性获取整个数据。

2 个答案:

答案 0 :(得分:0)

请删除@NamedAttributeNode 到许多enter image description here,并尝试调试org.hibernate.hql.internal.ast.QueryTranslatorImplenter image description here 。请注意RowSelection。我修复了'bug'

答案 1 :(得分:0)


@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "cd_order")
@Where(clause = "deleted = " + UN_DELETED)
@NamedEntityGraphs(value = {
        @NamedEntityGraph(name = "findAll", attributeNodes =
                {@NamedAttributeNode(value = "company"),
                        @NamedAttributeNode(value = "partnerCompany"),
//                   remove one to many ,   @NamedAttributeNode(value = "orderOssUrls"),
                        @NamedAttributeNode(value = "patient")
                })}
)
public class Order extends BaseEntity {

    @Column(name = "patient_id", insertable = false, updatable = false, columnDefinition = " BIGINT NOT NULL  COMMENT '订单id' ")
    private Long patientId;

    @OneToOne(fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), name = "patient_id", referencedColumnName = "id")
    private Patient patient;

    @Column(name = "patient_name", columnDefinition = " VARCHAR(48) COMMENT '案例编号' ")
    private String patientName;

    @Column(name = "case_code", columnDefinition = " VARCHAR(48) NOT NULL  COMMENT '案例编号' ")
    private String caseCode;

    @Column(name = "company_id", insertable = false, updatable = false, columnDefinition = " BIGINT  COMMENT 'companyId' ")
    private Long companyId;

    @OneToOne(fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), name = "company_id", referencedColumnName = "id")
    @NotFound(action = NotFoundAction.IGNORE)
    private Company company;

    @Column(name = "partner_company_id", insertable = false, updatable = false, columnDefinition = " BIGINT  COMMENT '合作伙伴的companyId' ")
    private Long partnerCompanyId;

    @OneToOne(fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), name = "partner_company_id", referencedColumnName = "id")
    @NotFound(action = NotFoundAction.IGNORE)
    private Company partnerCompany;

    @Column(name = "order_id", columnDefinition = " BIGINT NOT NULL  COMMENT '订单id' ")
    private Long orderId;

    @Fetch(FetchMode.JOIN)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), name = "order_id", referencedColumnName = "order_id")
    @OneToMany(fetch = FetchType.LAZY)
    private List<OrderOssUrl> orderOssUrls;

    @Column(name = "recovery_info", columnDefinition = " VARCHAR(128)  COMMENT '恢复信息' ")
    private String recoveryInfo;

    /**
     * 交货日期
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "submit_time", columnDefinition = "  datetime COMMENT '请求交货日期' ")
    private LocalDateTime submitTime;

    /**
     * 订购日期
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "order_time", columnDefinition = "  datetime COMMENT '订购日期' ")
    private LocalDateTime orderTime;

    /**
     * 扫描日期
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "scan_time", columnDefinition = "  datetime COMMENT '扫描日期' ")
    private LocalDateTime scanTime;

    @Enumerated(value = EnumType.STRING)
    @Column(name = "order_type", columnDefinition = " VARCHAR(20) NOT NULL COMMENT '订单类型' ")
    private OrderTypeEnum orderType;

    @Enumerated(value = EnumType.STRING)
    @Column(name = "cc_order_status", columnDefinition = " VARCHAR(20)   COMMENT '诊所订单状态' ")
    private OrderStatusEnum ccOrderStatus;

    @Enumerated(value = EnumType.STRING)
    @Column(name = "lab_order_status", columnDefinition = " VARCHAR(20)   COMMENT '工厂订单状态' ")
    private OrderStatusEnum labOrderStatus;

    /**
     * @param orderType 订单类型
     */
    public void init(@NonNull OrderTypeEnum orderType) {
        generateCaseCode();

        if (orderType == OrderTypeEnum.CLINIC) {
            this.setCcOrderStatus(OrderStatusEnum.NEW);
        } else {
            this.setLabOrderStatus(OrderStatusEnum.NEW);
        }
        this.setOrderType(orderType);
        this.setOrderId(GlobalUniqueIdGeneratorUtils.generateOrderId());
    }

    /**
     * 设置案例编号
     */
    private void generateCaseCode() {
        this.setCaseCode(getYMDStrCurrently() + "-" + this.getPatient().getName());
    }

}


public interface OrderRepository extends BaseRepository<Order, Long> {


    @NonNull
    @EntityGraph(value = "findAll", type = EntityGraph.EntityGraphType.LOAD)
    Page<Order> findAll(Specification<Order> specification, @NonNull Pageable pageable);
}

sql

select order0_.id                   as id1_5_0_,
       patient1_.id                 as id1_9_1_,
       company2_.id                 as id1_1_2_,
       company3_.id                 as id1_1_3_,
       order0_.create_time          as create_t2_5_0_,
       order0_.deleted              as deleted3_5_0_,
       order0_.update_time          as update_t4_5_0_,
       order0_.version              as version5_5_0_,
       order0_.case_code            as case_cod6_5_0_,
       order0_.cc_order_status      as cc_order7_5_0_,
       order0_.company_id           as company_8_5_0_,
       order0_.lab_order_status     as lab_orde9_5_0_,
       order0_.order_id             as order_i10_5_0_,
       order0_.order_time           as order_t11_5_0_,
       order0_.order_type           as order_t12_5_0_,
       order0_.partner_company_id   as partner13_5_0_,
       order0_.patient_id           as patient14_5_0_,
       order0_.patient_name         as patient15_5_0_,
       order0_.recovery_info        as recover16_5_0_,
       order0_.scan_time            as scan_ti17_5_0_,
       order0_.submit_time          as submit_18_5_0_,
       patient1_.create_time        as create_t2_9_1_,
       patient1_.deleted            as deleted3_9_1_,
       patient1_.update_time        as update_t4_9_1_,
       patient1_.version            as version5_9_1_,
       patient1_.activity_time      as activity6_9_1_,
       patient1_.day                as day7_9_1_,
       patient1_.email              as email8_9_1_,
       patient1_.month              as month9_9_1_,
       patient1_.name               as name10_9_1_,
       patient1_.note               as note11_9_1_,
       patient1_.phone              as phone12_9_1_,
       patient1_.sex                as sex13_9_1_,
       patient1_.year               as year14_9_1_,
       company2_.create_time        as create_t2_1_2_,
       company2_.deleted            as deleted3_1_2_,
       company2_.update_time        as update_t4_1_2_,
       company2_.version            as version5_1_2_,
       company2_.additional_info_id as addition6_1_2_,
       company2_.address            as address7_1_2_,
       company2_.biz_type           as biz_type8_1_2_,
       company2_.cell_phone         as cell_pho9_1_2_,
       company2_.city               as city10_1_2_,
       company2_.country_code       as country11_1_2_,
       company2_.country_name       as country12_1_2_,
       company2_.description        as descrip13_1_2_,
       company2_.icon               as icon14_1_2_,
       company2_.language           as languag15_1_2_,
       company2_.mechanism_id       as mechani16_1_2_,
       company2_.name               as name17_1_2_,
       company2_.office_phone       as office_18_1_2_,
       company2_.postcode           as postcod19_1_2_,
       company2_.province           as provinc20_1_2_,
       company2_.time_zone          as time_zo21_1_2_,
       company3_.create_time        as create_t2_1_3_,
       company3_.deleted            as deleted3_1_3_,
       company3_.update_time        as update_t4_1_3_,
       company3_.version            as version5_1_3_,
       company3_.additional_info_id as addition6_1_3_,
       company3_.address            as address7_1_3_,
       company3_.biz_type           as biz_type8_1_3_,
       company3_.cell_phone         as cell_pho9_1_3_,
       company3_.city               as city10_1_3_,
       company3_.country_code       as country11_1_3_,
       company3_.country_name       as country12_1_3_,
       company3_.description        as descrip13_1_3_,
       company3_.icon               as icon14_1_3_,
       company3_.language           as languag15_1_3_,
       company3_.mechanism_id       as mechani16_1_3_,
       company3_.name               as name17_1_3_,
       company3_.office_phone       as office_18_1_3_,
       company3_.postcode           as postcod19_1_3_,
       company3_.province           as provinc20_1_3_,
       company3_.time_zone          as time_zo21_1_3_
from cd_order order0_
         left outer join cd_patient patient1_ on order0_.patient_id = patient1_.id
         left outer join cd_company company2_ on order0_.partner_company_id = company2_.id
         left outer join cd_company company3_ on order0_.company_id = company3_.id
where (order0_.deleted = 0)
  and order0_.order_id >= 1
  and (order0_.case_code like '%case%')
  and patient1_.year >= 1
  and patient1_.month >= 1
  and patient1_.day >= 1
  and patient1_.sex = 'MALE'
  and (patient1_.phone like '%phone%')
  and (patient1_.name like '%name%')
  and (order0_.update_time between '2021-06-23 14:40:53.101831' and '2021-06-27 14:40:53.101944')
order by order0_.create_time asc, order0_.submit_time desc
limit 10