Spring Data Rest为集合

时间:2018-02-26 10:36:42

标签: spring spring-data-jpa spring-data-rest

我有两个具有一对多关系的实体:

public class User {
    @Id
    @GeneratedValue
    private Long id;
    private String username;
    private String phoneNumber;
    private Timestamp creationDate;
    @OneToMany(mappedBy = "user")
    private List<Role> roles;

}

public class Role {
    @Id
    @GeneratedValue
    private long id;
    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

}

但是当我调用get方法加载用户信息时,我可以在日志文件中看到还要调用用于检索用户角色的附加查询。如何通过弹簧数据休息来防止它?

1 个答案:

答案 0 :(得分:1)

将FetchType定义为<table datatable [dtOptions]="dtOptions"> <thead> <tr> <th>ID</th> <th>First name</th> <th>Last name</th> </tr> </thead> <tbody> <tr *ngFor="let person of persons"> <td>{{ person.id }}</td> <td>{{ person.firstName }}</td> <td>{{ person.lastName }}</td> </tr> </tbody> </table> ,如下所示。

LAZY

默认情况下,ManyToOne的JPA fetchType为@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) private User user; 。请参阅here