org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.wolf.a_StudentManageSystem.domain.Teacher.courses, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:316)
at java.lang.String.valueOf(String.java:2854)
at java.lang.StringBuilder.append(StringBuilder.java:128)
at com.wolf.a_StudentManageSystem.domain.Teacher.toString(Teacher.java:28)
at java.lang.String.valueOf(String.java:2854)
at java.io.PrintStream.println(PrintStream.java:821)
at com.wolf.a_StudentManageSystem.DaoImpl.TeacherDaoImplTest.testGetById(TeacherDaoImplTest.java:35)
懒加载问题:
Hibernate在get对象时,如果含有集合或者实体,会懒加载。
可以在集合的属性配置里,设置 lazy="false",或者实体类属性配置为lazy="false"。