Spring 和 Hibernate:为什么获取 1 个惰性字段会触发加载所有其他惰性字段

Mr.*_*mes 5 java spring hibernate lazy-loading spring-data-jpa

在我的应用程序中,我有一个Staff具有许多这样的延迟加载字段的实体。

@Entity(name="CommonStaff")
@Table(name="staff")
@Getter @Setter
public class Staff implements Serializable {
    ...

    @ManyToOne(fetch=FetchType.LAZY)
    @LazyToOne(LazyToOneOption.NO_PROXY)
    @JoinColumn(name="nationality", referencedColumnName="code", insertable=false, updatable=false)
    private Nationality nationality;

    @ManyToOne(fetch=FetchType.LAZY)
    @LazyToOne(LazyToOneOption.NO_PROXY)
    @JoinColumn(name="marital_status", referencedColumnName="code", insertable=false, updatable=false)
    private MaritalStatus maritalStatus;

    ...
}
Run Code Online (Sandbox Code Playgroud)

当我加载Staff记录时,这些字段都不会按预期加载。然而,当我触发时,例如,getNationality()我看到框架MaritalStatus也执行 SQL 来加载。我一直在尝试找到解决此问题的方法,但找不到任何有用的资源。如果您能为我指明方向,我将不胜感激。

一些示例代码。

@Autowired
@Qualifier("dataModuleStaffRepo")
private StaffRepo staffRepo;

@CustomerTransactional
@GetMapping("/profile")
public void testProfile(@RequestParam String userId) {
    Optional<Staff> staff = staffRepo.findByUserId(userId);
    if (staff.isPresent()) {
        System.out.println(staff.get().getName());
        System.out.println(staff.get().getNationality().getName());
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我在控制台中看到的内容。打印出工作人员的姓名后,也getNationality()触发了加载。MaritalStatus

Edgar Rey Tann
2020-04-16 14:29:40,283 DEBUG [http-nio-9000-exec-2] org.hibernate.SQL   : 
    /* sequential select
        com.ft.common.db.customer.domain.Staff */ select
            staff_.marital_status as marital19_23_,
            staff_.nationality as nationa22_23_
        from
            staff staff_ 
        where
            staff_.id=?
2020-04-16 14:29:40,283 TRACE [http-nio-9000-exec-2] org.hibernate.type.descriptor.sql.BasicBinder   : binding parameter [1] as [BIGINT] - [660]
2020-04-16 14:29:40,291 DEBUG [http-nio-9000-exec-2] org.hibernate.SQL   : 
    /* load com.ft.common.db.customer.domain.MaritalStatus */ select
        maritalsta0_.id as id1_9_0_,
        maritalsta0_.code as code2_9_0_,
        maritalsta0_.description as descript3_9_0_,
        maritalsta0_.name as name4_9_0_,
        maritalsta0_.order_id as order_id5_9_0_,
        maritalsta0_.short_name as short_na6_9_0_ 
    from
        marital_status maritalsta0_ 
    where
        maritalsta0_.code=?
2020-04-16 14:29:40,291 TRACE [http-nio-9000-exec-2] org.hibernate.type.descriptor.sql.BasicBinder   : binding parameter [1] as [VARCHAR] - [MAR_2]
2020-04-16 14:29:40,298 DEBUG [http-nio-9000-exec-2] org.hibernate.SQL   : 
    /* load com.ft.common.db.customer.domain.Nationality */ select
        nationalit0_.id as id1_16_0_,
        nationalit0_.code as code2_16_0_,
        nationalit0_.description as descript3_16_0_,
        nationalit0_.name as name4_16_0_,
        nationalit0_.order_id as order_id5_16_0_,
        nationalit0_.short_name as short_na6_16_0_ 
    from
        nationality nationalit0_ 
    where
        nationalit0_.code=?
2020-04-16 14:29:40,298 TRACE [http-nio-9000-exec-2] org.hibernate.type.descriptor.sql.BasicBinder   : binding parameter [1] as [VARCHAR] - [NAT_I]
Indonesian
Run Code Online (Sandbox Code Playgroud)

Ekl*_*vya 4

默认情况下,实体类的所有惰性属性都属于名为 DEFAULT 的组。获取 DEFAULT 组的任何属性也会获取其他属性。为了解决这个问题,我们需要定义我们希望使用@LazyGroup注释单独获取的组。

因此,我们使用 @LazyGroup 注释来注释国籍和 maritalStatus,如下所示。

@ManyToOne(fetch=FetchType.LAZY)
@LazyToOne(LazyToOneOption.NO_PROXY)
@LazyGroup("nationality")
@JoinColumn(name="nationality", referencedColumnName="code", insertable=false, updatable=false)
private Nationality nationality;

@ManyToOne(fetch=FetchType.LAZY)
@LazyToOne(LazyToOneOption.NO_PROXY)
@LazyGroup("maritalStatus")
@JoinColumn(name="marital_status", referencedColumnName="code", insertable=false, updatable=false)
private MaritalStatus maritalStatus;
Run Code Online (Sandbox Code Playgroud)

希望您正在使用字节码增强来进行无代理延迟获取