java – Spring Data REST HATEOS:不是延迟加载
作者:互联网
问题
我定义了两个实体:学校和学区.一个地区可以有许多学校,一所学校可以属于一个学区.
当对这个端点http:// localhost:8080 /区执行GET请求时,我想获得所有区的列表,而不是获取每个区的相关学校集.但似乎无论我做什么,hibernate都会调用DB来为每个学校单独获取数据.
实体
学校
@Getter
@Setter
@NoArgsConstructor
@Entity
public class School {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column(unique=true)
private Long number;
@NotNull
@Column
private String name;
@NotNull
private boolean closed;
@Embedded
private ContactInfo contactInfo;
private String gradeLow;
private String gradeHigh;
private int enrollment;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "district_id")
private District district;
}
区
@Getter
@Setter
@NoArgsConstructor
@Entity
public class District {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true)
private Integer number;
private String name;
private String type;
private int enrollment;
private Date updated;
@Embedded
private ContactInfo contactInfo;
@Getter(AccessLevel.NONE)
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "district")
private Set<School> schoolList;
}
日志输出
SELECT district0_.id AS id1_5_,
district0_.city AS city2_5_,
district0_.email AS email3_5_,
district0_.fax AS fax4_5_,
district0_.first_name AS first_na5_5_,
district0_.last_name AS last_nam6_5_,
district0_.name_prefix AS name_pre7_5_,
district0_.phone AS phone8_5_,
district0_.state AS state9_5_,
district0_.street AS street10_5_,
district0_.title AS title11_5_,
district0_.website AS website12_5_,
district0_.zip AS zip13_5_,
district0_.enrollment AS enrollm14_5_,
district0_.NAME AS name15_5_,
district0_.number AS number16_5_,
district0_.type AS type17_5_,
district0_.updated AS updated18_5_
FROM district district0_
SELECT schoollist0_.district_id AS distric20_7_0_,
schoollist0_.id AS id1_7_0_,
schoollist0_.id AS id1_7_1_,
schoollist0_.closed AS closed2_7_1_,
schoollist0_.city AS city3_7_1_,
schoollist0_.email AS email4_7_1_,
schoollist0_.fax AS fax5_7_1_,
schoollist0_.first_name AS first_na6_7_1_,
schoollist0_.last_name AS last_nam7_7_1_,
schoollist0_.name_prefix AS name_pre8_7_1_,
schoollist0_.phone AS phone9_7_1_,
schoollist0_.state AS state10_7_1_,
schoollist0_.street AS street11_7_1_,
schoollist0_.title AS title12_7_1_,
schoollist0_.website AS website13_7_1_,
schoollist0_.zip AS zip14_7_1_,
schoollist0_.district_id AS distric20_7_1_,
schoollist0_.enrollment AS enrollm15_7_1_,
schoollist0_.grade_high AS grade_h16_7_1_,
schoollist0_.grade_low AS grade_l17_7_1_,
schoollist0_.NAME AS name18_7_1_,
schoollist0_.number AS number19_7_1_
FROM school schoollist0_
WHERE schoollist0_.district_id = ?
SELECT schoollist0_.district_id AS distric20_7_0_,
schoollist0_.id AS id1_7_0_,
schoollist0_.id AS id1_7_1_,
schoollist0_.closed AS closed2_7_1_,
schoollist0_.city AS city3_7_1_,
schoollist0_.email AS email4_7_1_,
schoollist0_.fax AS fax5_7_1_,
schoollist0_.first_name AS first_na6_7_1_,
schoollist0_.last_name AS last_nam7_7_1_,
schoollist0_.name_prefix AS name_pre8_7_1_,
schoollist0_.phone AS phone9_7_1_,
schoollist0_.state AS state10_7_1_,
schoollist0_.street AS street11_7_1_,
schoollist0_.title AS title12_7_1_,
schoollist0_.website AS website13_7_1_,
schoollist0_.zip AS zip14_7_1_,
schoollist0_.district_id AS distric20_7_1_,
schoollist0_.enrollment AS enrollm15_7_1_,
schoollist0_.grade_high AS grade_h16_7_1_,
schoollist0_.grade_low AS grade_l17_7_1_,
schoollist0_.NAME AS name18_7_1_,
schoollist0_.number AS number19_7_1_
FROM school schoollist0_
WHERE schoollist0_.district_id = ?
SELECT schoollist0_.district_id AS distric20_7_0_,
schoollist0_.id AS id1_7_0_,
schoollist0_.id AS id1_7_1_,
schoollist0_.closed AS closed2_7_1_,
schoollist0_.city AS city3_7_1_,
schoollist0_.email AS email4_7_1_,
schoollist0_.fax AS fax5_7_1_,
schoollist0_.first_name AS first_na6_7_1_,
schoollist0_.last_name AS last_nam7_7_1_,
schoollist0_.name_prefix AS name_pre8_7_1_,
schoollist0_.phone AS phone9_7_1_,
schoollist0_.state AS state10_7_1_,
schoollist0_.street AS street11_7_1_,
schoollist0_.title AS title12_7_1_,
schoollist0_.website AS website13_7_1_,
schoollist0_.zip AS zip14_7_1_,
schoollist0_.district_id AS distric20_7_1_,
schoollist0_.enrollment AS enrollm15_7_1_,
schoollist0_.grade_high AS grade_h16_7_1_,
schoollist0_.grade_low AS grade_l17_7_1_,
schoollist0_.NAME AS name18_7_1_,
schoollist0_.number AS number19_7_1_
FROM school schoollist0_
WHERE schoollist0_.district_id = ?
SELECT schoollist0_.district_id AS distric20_7_0_,
schoollist0_.id AS id1_7_0_,
schoollist0_.id AS id1_7_1_,
schoollist0_.closed AS closed2_7_1_,
schoollist0_.city AS city3_7_1_,
schoollist0_.email AS email4_7_1_,
schoollist0_.fax AS fax5_7_1_,
schoollist0_.first_name AS first_na6_7_1_,
schoollist0_.last_name AS last_nam7_7_1_,
schoollist0_.name_prefix AS name_pre8_7_1_,
schoollist0_.phone AS phone9_7_1_,
schoollist0_.state AS state10_7_1_,
schoollist0_.street AS street11_7_1_,
schoollist0_.title AS title12_7_1_,
schoollist0_.website AS website13_7_1_,
schoollist0_.zip AS zip14_7_1_,
schoollist0_.district_id AS distric20_7_1_,
schoollist0_.enrollment AS enrollm15_7_1_,
schoollist0_.grade_high AS grade_h16_7_1_,
schoollist0_.grade_low AS grade_l17_7_1_,
schoollist0_.NAME AS name18_7_1_,
schoollist0_.number AS number19_7_1_
FROM school schoollist0_
WHERE schoollist0_.district_id = ?
....
尽管将schoolList配置为延迟加载,但如上所示,SELECT FROM school重复了数百次.
版本:
springBootVersion = '1.4.2.RELEASE'
hibernate-core:5.0.11
'org.springframework.boot:spring-boot-starter-data-jpa'
'org.springframework.boot:spring-boot-starter-data-rest'
'org.springframework.boot:spring-boot-starter-web'
'org.springframework.boot:spring-boot-starter-actuator'
'org.springframework.boot:spring-boot-starter-hateoas'
'org.springframework.boot:spring-boot-starter-security'
解决方法:
看起来你的代码甚至不可行,在没有@JsonIgnore的情况下,在尝试获得所有可用的学校时你应该收到类似的东西:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No
serializer found for class
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no
properties discovered to create BeanSerializer (to avoid exception,
disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference
chain:
java.util.ArrayList[0]->com.mberazouski.stackoverflow.springboothibernate.model.School[“district”]->com.mberazouski.stackoverflow.springboothibernate.model.District_$$_jvstad5_0[“handler”])
at
com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
~[jackson-databind-2.9.6.jar:2.9.6]
但是如果你要添加这个注释 – 一切都应该按预期开始工作.所以应该添加的唯一变化应该是:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "district_id")
@JsonIgnore
private District district;
跟踪的结果将是:
2018-06-17 17:27:56.431 DEBUG 25024 — [nio-8080-exec-1]
org.hibernate.SQL : select school0_.id as
id1_3_, school0_.closed as closed2_3_, school0_.district_id as
district8_3_, school0_.enrollment as enrollme3_3_, school0_.grade_high
as grade_hi4_3_, school0_.grade_low as grade_lo5_3_, school0_.name as
name6_3_, school0_.number as number7_3_ from school school0_
与此相反,如果你将删除懒惰:
@ManyToOne
@JoinColumn(name = "district_id")
private District district;
用于测试的控制器:
import com.mberazouski.stackoverflow.springboothibernate.model.School;
import com.mberazouski.stackoverflow.springboothibernate.repository.SchoolRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class SchoolController {
@Autowired
SchoolRepository schoolRepository;
@GetMapping("/schools")
public List<School> getAllSchools() {
return schoolRepository.findAll();
}
}
希望建议的更改将解决您的问题.
祝好运.
标签:java,spring,spring-data-rest,hibernate,spring-hateoas 来源: https://codeday.me/bug/20190910/1800463.html