spring休眠延迟加载与休眠 [英] spring rest lazy loading with hibernate
问题描述
在谷歌搜索后,我还没有找到解决方案来延迟加载。
我有两个实体,如下所示:
University.java
<$ c
@Table(schema =core,name =university)
public class大学扩展BaseEntity {
private String uniName;
private String uniTelephon;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(fetch = FetchType.LAZY,mappedBy =university,cascade = CascadeType.ALL)
@JsonManagedReference
私人列表<学生>学生们;
// setter和getter
}
Student.java
@Entity
@Table(schema =core,name =student)
public class Student {
@Id
@GeneratedValue
私人长ID;
private String firstName;
私人字符串姓氏;
私有字符串部分;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =UNIVERSITY_ID,nullable = false)
@JsonBackReference
私立大学;
// setter and getter
}
任何我的休息结束点
@GetMapping(/ list)
public ResponseEntity list()抛出异常{
//我需要只返回大学但它返回它热烈地与他们的学生
返回新的ResponseEntity(this.universityService.findAll(),HttpStatus.OK);
}
在调用其余api后,它会让所有学生返回大学。
有没有办法告诉杰克逊不要序列化未提取的对象或集合?
有人可以帮助我证明解决方案?
尝试添加以下依赖项(取决于您的hibernate版本):
< dependency>
< groupId> com.fasterxml.jackson.datatype< / groupId>
< artifactId> jackson-datatype-hibernate5< / artifactId>
< version> $ {jackson.version}< / version>
< /依赖关系>
然后(假设您有基于Java的配置)在WebMvcConfigurerAdapter类中添加以下内容:
@Override
public void configureMessageConverters(List< HttpMessageConverter<>>转换器){
转换器。添加(jackson2HttpMessageConverter());
}
@Bean
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter(){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(this.jacksonBuilder()。build());
返回转换器;
}
public Jackson2ObjectMapperBuilder jacksonBuilder(){
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
Hibernate5Module hibernateModule = new Hibernate5Module();
hibernateModule.configure(Feature.FORCE_LAZY_LOADING,false);
builder.modules(hibernateModule);
// Spring MVC默认的Objectmapper配置
builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
builder.featuresToDisable(MapperFeature.DEFAULT_VIEW_INCLUSION);
回报建造者;
}
它应该强制Jackson的objectMapper不读取延迟加载的值。
I am trying to develop spring rest api with hibernate. after searching in google, I have not find solution to lazy loading. I have two entity like below:
University.java
@Entity()
@Table(schema = "core", name = "university")
public class University extends BaseEntity {
private String uniName;
private String uniTelephon;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "university", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Student> students;
//setter and getter
}
Student.java
@Entity
@Table(schema = "core",name = "student")
public class Student {
@Id
@GeneratedValue
private long id;
private String firstName;
private String lastName;
private String section;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "UNIVERSITY_ID",nullable = false)
@JsonBackReference
private University university;
// setter and getter
}
any my rest end point
@GetMapping("/list")
public ResponseEntity list() throws Exception {
// I need to return just Universities But it return it eagerly with their students
return new ResponseEntity(this.universityService.findAll(), HttpStatus.OK);
}
after calling the rest api, it return university with all students.
There is a way to tell Jackson to not serialize the unfetched objects or collections?
Can somebody help me with a proved solution?
Try adding the following dependancy (depending on your hibernate version):
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>${jackson.version}</version>
</dependency>
And then (assuming you have a Java based configuration) add the following in the WebMvcConfigurerAdapter class:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jackson2HttpMessageConverter());
}
@Bean
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(this.jacksonBuilder().build());
return converter;
}
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
Hibernate5Module hibernateModule = new Hibernate5Module();
hibernateModule.configure(Feature.FORCE_LAZY_LOADING, false);
builder.modules(hibernateModule);
// Spring MVC default Objectmapper configuration
builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
builder.featuresToDisable(MapperFeature.DEFAULT_VIEW_INCLUSION);
return builder;
}
It should force the Jackson's objectMapper to not fetch lazy-loaded values.
这篇关于spring休眠延迟加载与休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!