2
我試圖發展與休眠春天REST API。 在谷歌搜索後,我還沒有找到解決延遲加載。 我有兩個實體,如下圖所示:彈簧安置延遲加載和Hibernate
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
}
任我休息終點
@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);
}
調用REST API後,它與所有學生一起返回大學。
有一種方式來告訴傑克遜不序列化未提取對象或集合?
有人可以幫助我已驗證解決方案?
其通過以下異常HTTP狀態500 - 無法寫入內容:無法懶惰地初始化角色集合:a2.asena.cashModel.University.students,無法初始化代理 - 無會話(通過引用鏈:java。 util.ArrayList [0] - > a2.asena.cashModel.University [ 「學生」]);嵌套的異常是com.fasterxml.jackson.databind.JsonMappingException:未能懶惰地初始化一個角色集合:a2.asena.cashModel.University.students,無法初始化代理 - 沒有會話(通過引用鏈:java.util.ArrayList [ 0] - > a2.asena.cashModel.University [「students」]) – Generic
for above exception solution is overriding Filter in Appinitializer @Override protected Filter [] getServletFilters(){ return new Filter [] {new HiddenHttpMethodFilter(),新MultipartFilter(),新OpenEntityManagerInViewFilter()};是 – Generic
一些文章說這是不正確的解決方案 – Generic