如何正确公开延迟加载的多个字段,以便用户可以在Spring Data REST中获取/ PATCH / POST / DELETE许多实体关系?
例如,给定一个由多对多关系绑定的Student实体和Teacher实体,使用以下POJO:
@Entity
public class Teacher { // owner of bidirectional relationship
@Id
private int id;
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "teacher_student",joinColumns = @JoinColumn(name = "teacher_id"),inverseJoinColumns = @JoinColumn(name = "student_id"))
private Set
实体被赋予存储库:
@RepositoryRestResource(path = "teacher")
public interface TeacherRepository extends CrudRepository
当我发送GET到localhost:8080 /老师时,我得到:
"_embedded": {
"teacher": [
{
"name": "Bill Billie","_links": {
"self": { "href": "http://localhost:8080/teacher/1" },"teacher": { ... },"students": { "href": "http://localhost:8080/teacher/1/students" }
}},(more teachers here...)
]
}
...
但是,当我尝试GET到http://localhost:8080/teacher/1/students时,我得到了404 Not Found,即使老师“Bill Billie”确实有一个学生在数据库中与他相关联.
有趣的是,如果我将FetchType更改为FetchType.EAGER,一切正常,我可以执行预期的GET,PATCH等.是什么给出的?这可能是一个错误,还是我搞砸了什么?
tl; dr许多关系没有通过延迟提取正确暴露,但在急切提取时工作正常.我怎么能懒得拿起它?
编辑:如果重要,我使用Spring 4.2.6和Spring Boot 1.3.5,OpenJPA 2.4.1作为我的JPA提供者.
最佳答案