Spring Data JPA Lazy Fetch不适用于@OneToOne双向

时间:2017-11-17 10:47:42

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

我在Spring Boot中使用Spring Data JPA。 即使我使用了Lazy Fetch,Hibernate也会为实体中的所有关联生成SQL。 我也使用了@OneToOne双向映射。两个方向都使用了LAZY fetch。当我从Parent-> Child访问数据时,它没有加载Child。但是Child->父母急切地加载它。我查了一下,它为关联建立了sqls。真的很困惑。

父实体:员工:

@Entity
@Table(name = "employee")
public class Employee {
    @Id
    @Column(name = "employee_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer employeeId;
    @Column(name = "employee_name")
    private String employeeName;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "company_id")
    private Company company;
    //setters and getters
}

子实体:地址

@Entity
@Table(name = "address")
public class Address{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "address_id")
    private Integer addressId;
    @Column(name = "address_name")
    private String addressName;
    @OneToOne(mappedBy = "address", fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = { "address"})
    private Employee employee;
    //getters and setters
}

我在两种情况下都使用了repository.findOne(id)。

对于Parent-> Child:我得到了结果和sqls如下: SQL:

Hibernate: select employee0_.employee_id as employee1_1_0_, employee0_.company_id as company_5_1_0_, employee0_.employee_desgn as employee2_1_0_, employee0_.employee_email as employee3_1_0_, employee0_.employee_name as employee4_1_0_ from employee employee0_ where employee0_.employee_id=?

输出:

[{" EMPLOYEEID&#34:4," employeeName":"格利扬""地址":空,"项目& #34;:空}]

来自Child-> Parent:我得到了结果和sqls如下: SQL:

Hibernate: select address0_.address_id as address_1_0_0_, address0_.address_name as address_2_0_0_ from address address0_ where address0_.address_id=?
Hibernate: select employee0_.employee_id as employee1_1_0_, employee0_.address_id as address_5_1_0_, employee0_.employee_desgn as employee2_1_0_, employee0_.employee_email as employee3_1_0_, employee0_.employee_name as employee4_1_0_ from employee employee0_ where employee0_.address_id=?

输出:

{" addressId":2" addressName":" mykukun""雇员" {" EMPLOYEEID&#34 ; 3" employeeName":" Krishna3"}}

请告诉我,我需要做些什么让Lazy Fetch从Child到Parent工作? 感谢

0 个答案:

没有答案