【发布时间】:2021-06-15 02:55:01
【问题描述】:
我一直在试图弄清楚如何让它与 Lazy fetch 类型一起使用,但我无法完全弄清楚。
@Entity
@Table(name = "module")
public class Module extends ReservationOption{
@Column
private String name;
@Column
private int credits;
@Column
private int weeks;
@Column(name = "no_of_lectures")
private int noOfLectures;
@Column(name ="no_of_practicals")
private int noOfPracticals;
@Column(name = "lecture_length")
private int lectureLength;
@Column(name = "practical_length")
private int practicalLength;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "takes",
joinColumns = @JoinColumn(name = "moduleID"),
inverseJoinColumns = @JoinColumn(name = "studentID")
)
private Set<Student> students = new HashSet<>();
public void addTakes(Student student){
IDatabase db = Database.getInstance();
if(!db.checkInTable(Student.class, student.getId())) db.add(student);
Hibernate.initialize(students);
students.add(student);
student.getModules().add(this);
db.update(this);
}
@Entity
@Table(name = "student")
public class Student extends Person{
/**
* Constructor for Student class, allows for object creation
* @param id From Person
* @param firstName From Person
* @param lastName From Person
*/
public Student(String id, String firstName, String lastName) {
super(id, firstName, lastName);
}
/**
* Blank constructor to allow for database interfacing
*/
public Student(){
super();
}
@ManyToMany(mappedBy = "students", fetch = FetchType.LAZY)
private Set<Module> modules = new HashSet<>();
@Override
public Set<Module> getModules() {
return modules;
}
public void setModules(Set<Module> modules) {
this.modules = modules;
}
}
错误被抛出 Hibernate.initialize(students);或者在下一行(如果不存在),在学生集上,标题中有错误。任何帮助(不仅仅是将 fetch type 设置为 Eager)将不胜感激。
【问题讨论】:
标签: java hibernate lazy-initialization