[英]Fetching & Updating lazy-loaded many-many fields in Spring Data REST
How do I correctly expose lazy-loaded many-many fields so that users can GET
/ PATCH
/ POST
/ DELETE
many-many entity relationships in Spring Data REST? 如何正确显示延迟加载的许多字段,以便用户可以在Spring Data REST中GET
/ PATCH
/ POST
/ DELETE
多个实体关系?
For example, given a Student
entity and Teacher
entity bound by a many to many relationship, with the following POJOs: 例如,给定一个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<Student> students;
// Constructor, getters/setters...
}
@Entity
public class Student {
@Id
private int id;
private String name;
@ManyToMany(mappedBy = "students", fetch = FetchType.LAZY)
private Set<Teacher> teachers;
// Constructor, getters/setters...
}
The entities are given repositories: 实体具有存储库:
@RepositoryRestResource(path = "teacher")
public interface TeacherRepository extends CrudRepository<Teacher, int> {}
// similar repository for student
When I send a GET
to localhost:8080/teacher, I get: 当我将GET
发送到localhost:8080 / teacher时,我得到:
"_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...)
]
}
...
BUT , when I try a GET
to http://localhost:8080/teacher/1/students , I get a 404 Not Found , even though the teacher "Bill Billie" does have a student associated with him in the database. 但是 ,当我尝试GET
到http:// localhost:8080 / teacher / 1 / students时 ,我得到404 Not Found ,即使老师“ Bill Billie” 确实在数据库中有一个与他相关联的学生。
Interestingly, if I change the FetchType
to FetchType.EAGER
, everything works fine and I can perform the expected GET
, PATCH
, etc. What gives? 有趣的是,如果将FetchType
更改为FetchType.EAGER
,则一切正常,并且可以执行预期的GET
, PATCH
等。这有什么用? Is this a bug, perhaps, or am I screwing something up? 这是一个错误,还是我搞砸了?
tl;dr Many-many relations are not correctly exposed with lazy fetching, but work fine with eager fetching. tl; dr懒惰的访存无法正确暴露许多关系,但渴望的访存可以很好地工作。 How can I get lazy fetching to work with it? 我该如何懒惰地取用它?
Edit : If it matters, I am using Spring 4.2.6 with Spring Boot 1.3.5, and OpenJPA 2.4.1 as my JPA provider. 编辑 :如果有关系,我将Spring 4.2.6与Spring Boot 1.3.5和OpenJPA 2.4.1一起用作我的JPA提供程序。
Hmm I am not sure why it isn't auto-fetching someone more experienced would have to ask that, but you can specify a manual fetch with HQL's join fetch for each query. 嗯,我不确定为什么不自动获取经验更丰富的人会问这个问题,但是您可以为每个查询指定带有HQL的join fetch的手动获取。
select x from X left join fetch x.y y
After getting this working you can override your get statement with a specifically created controller as described here: Spring Data REST: Override repository method on the controller 完成此工作后,您可以按如下所述使用专门创建的控制器覆盖get语句: Spring Data REST:覆盖控制器上的存储库方法
Only other option I think might be worth trying is adding an @Lazy annotation on your Spring-data repository. 我认为可能值得尝试的唯一其他选择是在Spring数据存储库中添加@Lazy批注。
Try making your RestResources transactional. 尝试使RestResources具有事务性。
Annotate with @Transactional
用@Transactional
注释
Add the following dependency 添加以下依赖
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
</dependency>
Add the configuration class. 添加配置类。
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
//Registering Hibernate4Module to support lazy objects
mapper.registerModule(new Hibernate4Module());
messageConverter.setObjectMapper(mapper);
return messageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//Here we add our custom-configured HttpMessageConverter
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.