我有以下两个课程,
人类.java
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Human implements Serializable {
private long id;
private String name;
....
}
学生.java
@Entity
@DynamicUpdate
public class Student extends MyFactories {
private List<Know> KnowList;
@OneToMany(fetch = FetchType.LAZY)
public List<Know> getKnowlist() {
return knowlist;
}
public void setKnowlist(List<Know> KnowList) {
return Knowlist;
}
}
知道.java
@Entity
public class Know implements Serializable {
private long id;
private Human hu;
private Student st;
....
@ManyToOne
public Person getHu() {
return hu;
}
@ManyToOne
public Client getSt() {
return st;
}
.... setters .....
}
代码
Know kw = new Know();
kw.setSt(studentObject);
kw.setHu(humanObject);
session.save(kw);
tx.commit();
我能够插入到 Know 表中,但 hibernate 不会将任何记录插入到它创建的 student_know 表中。
我找到了这个答案,但它说如果我总是想检索所有记录,我需要使用该方法。我不知道(有时,我可能只需要检索学生类而不是其知道的列表)
System.out.println(this.student.getKnowList().size());
当我尝试访问列表时,它会遇到以下异常。
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myproject.Student.knowList, could not initialize proxy - no Session