即使某些字段是惰性的,休眠也会加载整个层次结构

时间:2019-08-24 13:44:13

标签: hibernate spring-boot spring-data-jpa

在JPQL中使用get延迟加载属性或使用fetch join时,即使它的某些字段也是惰性的,我也可以获得更多的惰性加载Class的整个层次结构。

该应用程序使用: -弹簧靴 -弹簧数据JPA 2.1.6 -休眠核心5.3.9 -我们认为虚假开放 -龙目岛2.1

@Entity
@Audited
@Builder
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class BT{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Setter(AccessLevel.NONE)
    private Long id;
    @ManyToMany
    private Set<PT> pt;
    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
    private Set<ST> s;

@Entity
@Audited
@Builder
@Data
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ST{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Setter(AccessLevel.NONE)
    private Long id;
    private Integer checkIntervalOffline;
    @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    private B b;
}


@Entity
@Audited
@Builder
@Data
@NoArgsConstructor(access = AccessLevel.PUBLIC  )
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class B{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Setter(AccessLevel.NONE)
    private Long id;
    @OneToMany(cascade = CascadeType.ALL)
    private Set<P> p;
}


@Entity
@Audited
@Builder
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class P{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Setter(AccessLevel.NONE)
    private Long id;
    @OneToMany(cascade = CascadeType.ALL)
    private Set<F> features;
}


@Entity
@Audited
@Builder
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class F{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Setter(AccessLevel.NONE)
    private Long id;
    private String value;
}

使用访存联接

 @Query(value = "SELECT b FROM BT b JOIN FETCH b.s")
    List<BundleTemplate> findAllWithSubscription();

使用get

public static BTDto btEntity2DtoWithS(BT entity) {
 entity.getSubscriptions();

我希望只得到与订阅相关的查询: 1.对于“获取加入”:

select bt.id as id1_13_0_, s.id as id1_57_1_, si2_.b_id as b_11_57_1_, 
from bt bt0_ 
inner join bt_st si1_ on bt0_.id=si1_.bt_id 
inner join st si2_ on si1_.s_id=si2_.id

  1. 为了获得类似的东西。

实际上,我对其他所有实体都有很多其他查询:

select b_.id as id1_2_0_ from b b0_ where b0_.id=?
select p0_.b_id as b_i1_6_0_, p0_.p_id as p2_6_0_, p1_.id as id1_35_1_product1_.pt_id as p_5_35_1_
from bp p0_ 
inner join p p1_ on p0_.p_id=p1_.id 
where p0_.b_id=?
and also for f
```

So somehow spring jpa tries to get the entire dependency tree for me ignoring the lazy.

0 个答案:

没有答案