Spring Data REST HATEOS:不是延迟加载

Fab*_*ian 5 java spring hibernate spring-data-rest spring-hateoas

问题

我定义了两个实体:SchoolDistrict。一个地区可以有许多学校,而一所学校可以属于一个地区。
GET对此端点执行请求时,http://localhost:8080/districts我想获取所有学区的列表,而无需获取每个学区的关联学校集。但是,无论我做什么,休眠似乎都在进行数据库调用以分别获取每个学校的数据。

实体

学校

@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;

} 
Run Code Online (Sandbox Code Playgroud)

@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;

}
Run Code Online (Sandbox Code Playgroud)

日志输出

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 = ? 

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

SELECT FROM school如上述所示,重复数百次,尽管其具有被配置为延迟加载schoolList。

版本:

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'
Run Code Online (Sandbox Code Playgroud)

Fab*_*ian 4

终于弄清楚了......为了简单起见,当我最初发布这个问题时,我没有包含所有代码。不幸的是,错误的代码不是我最初发布的。

发生了什么事

我为我的对象创建了一个投影School并将投影映射到 ,SchoolRepository如下面的代码所示。我认为只有在 REST 请求中明确指定时才应用投影(即:),/schools?projection=schoolExcerpt但显然,投影一直在应用。由于某种原因,该District对象将 应用于SchoolProjection每个关联的学校 -> 导致 SQL 查询从每个学校单独获取数据,即使存在注释也是如此@JsonIgore

我是如何解决这个问题的

通过简单地删除投影,我能够检索所有列表,Districts而无需进行数千次调用来解析每个相关的学校对象。

@RepositoryRestResource(excerptProjection = SchoolProjection.class)  //removing this line solves my problems
public interface SchoolRepository extends CrudRepository<School, Long>{    

}
Run Code Online (Sandbox Code Playgroud)

聚苯乙烯

我实际上甚至不需要@JsonIgnore注释... HATEOAS 足够聪明,不会包含相关对象 - 相反,它包含指向相关对象的链接。