我正在尝试使用Hibernate开发Spring REST API。 在谷歌上进行了搜索之后,我没有找到关于懒加载的解决方案。 我有两个实体,如下所示:
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
}
任何我的REST端点
@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 后,它返回包含所有学生的大学信息。
有没有办法告诉 Jackson 不要序列化未获取的对象或集合?
有人能帮我提供经过验证的解决方案吗?