org.hibernate.LazyInitializationException
1.org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.chen.vo.Teacher.students, no session or session was closed
异常描述:运行以下方法出现。注:持久类Teacher与Student呈多对多关联。
@Test//fail
public void query2(){
List list = hibernateTemplate.find("from Teacher t where t.id=69");
if(list.size()>0){
Teacher t = (Teacher) list.get(0);
Set<Student> ss = t.getStudents();
for (Student student : ss) {
System.out.println(student);
} }
System.out.println(list);
}
具体异常信息:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.chen.vo.Teacher.students, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:332)
at java.lang.String.valueOf(String.java:2854)
at java.io.PrintStream.println(PrintStream.java:821)
at com.chen.test.Many2ManyTest.query2(Many2ManyTest.java:248)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
解决方法:修改2个持久类的配置文件,在set处加上 lazy="false" cascade="all",many-to-many处加上 lazy="false"
1.修改Teacher.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.chen.vo.Teacher">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<!-- 通过table项告诉hibernate中间表的名称 -->
<set name="students" table="teacher_student" lazy="false" cascade="all">
<!-- 通过key属性告诉hibernate在中间表里面查询teacher_id值相应的teacher记录 -->
<key column="teacher_id" />
<!-- 通过column项告诉hibernate对student表中查找student_id值相就的studnet记录 -->
<many-to-many class="com.chen.vo.Student" column="student_id" lazy="false" />
</set>
</class>
</hibernate-mapping>
2.修改Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.chen.vo.Student" >
<id name="id" >
<generator class="native" />
</id>
<property name="name" />
<set name="teachers" table="teacher_student" lazy="false" cascade="all">
<key column="student_id" />
<many-to-many class="com.chen.vo.Teacher" column="teacher_id" lazy="false" />
</set>
</class>
</hibernate-mapping>
运行query2方法,正常!结果如下:(注:Student未显示老师原因在于在类Student中的toString方法中没有设置。)
Student [id=71, name=s1]
Student [id=72, name=s2]
[Teacher [id=69, name=t1, students=[Student [id=71, name=s1], Student [id=72, name=s2]]]]
小结:failed to lazily initialize a collection of role: com.chen.vo.Teacher.students这个异常信息说明异常出在懒加载上。
2.org.hibernate.LazyInitializationException: could not initialize proxy - no Session
异常描述:运行以下方法出现,注:执行这个方法是在已处理上面异常1的前提下,即执行query2正常,执行下面的query3却失败。
@Test//fail
public void query3(){
Teacher t = hibernateTemplate.load(Teacher.class, 69);
System.out.println(t);
// System.out.println(t.getStudents());
}
具体异常信息:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.chen.vo.Teacher_$$_javassist_0.getStudents(Teacher_$$_javassist_0.java)
at com.chen.test.Many2ManyTest.query3(Many2ManyTest.java:255)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
解决方法:
目前只有替换方法,1是将hibernateTemplate.load(Teacher.class, 69);这种查询换为query2中的hibernateTemplate.find("from Teacher t where t.id=69");或者直接获取Hibernate的原生session,如下所示:
public void query(int id){
Session s = null;
Transaction tx = null; SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
s = sessionFactory.openSession();
tx = s.beginTransaction();
Teacher t = (Teacher) s.get(Teacher.class, id);
System.out.println("students:"+t.getStudents());
System.out.println("students的个数:" + t.getStudents().size());
tx.commit();
s.close();
}
@Test
public void query(){
query(69);
}
小结:hibernateTemplate.load(Teacher.class, 69);为什么会出现此异常的原因及直接解决方式目前尚不知道,待解决..
org.hibernate.LazyInitializationException的更多相关文章
- org.hibernate.LazyInitializationException: failed to lazily initialize
今天搞了一上午,都在解决这个问题:org.hibernate.LazyInitializationException: failed to lazily initialize 原因很简单,是在非法的s ...
- (org.hibernate.LazyInitializationException:19) - could not initialize proxy错误
(org.hibernate.LazyInitializationException:19) - could not initialize proxy错误 在刚插入数据后,马上使用dao进行query ...
- 解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session懒载入问题
问题描写叙述: Struts Problem Report Struts has detected an unhandled exception: Messages: could not initia ...
- org.hibernate.LazyInitializationException...no session or session was closed
org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role:cn.its.oa ...
- J2EE进阶(九)org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session 前言 在<many-to-o ...
- jpa 解决org.hibernate.lazyinitializationexception could not initialize proxy - no session
org.hibernate.LazyInitializationException: could not initialize proxy [org.alan.entity.SysUser#1] - ...
- hibernate延迟加载org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javakc.hibernate.onetomany.entity.DeptEntity.emp, could not initialize proxy - no Session
public static void main(String[] args) { DeptEntity dept = getDept("402882e762ae888d0162ae888e ...
- Exception 07 : org.hibernate.LazyInitializationException: could not initialize proxy - no Session
异常名称: org.hibernate.LazyInitializationException: could not initialize proxy - no Session 异常截图: 异常详情: ...
- 解决方案,org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session org.hibernate.pro ...
随机推荐
- ios获取当前语言
上代码: + (NSString*)getPreferredLanguage { NSUserDefaults * defaults = [NSUserDefaults standardUserDef ...
- 第一次使用github很高端的赶脚
- html dom的加载
操作HTML DOM文档的一个难题是,你的JavaScript代码可能在DOM完全载入之前运行,这会导致你的代码产生一些问题.页面加载时浏览器内部操作的顺序大致是这样的: 1. HTML被解析. 2. ...
- js 获取 当前时间 时间差 时间戳 倒计时
开发web一段时间后发现经常使用时间进行一些操作,比较多的就是获取当前时间.对时间进行比较.时间倒计时.时间戳这些部分,每次去用经常忘总是需要去查询,还是自己总结一下比较靠谱. 获取时间戳的方法: 第 ...
- FreeCodeCamp-JS基础部分
---恢复内容开始--- .pop() 将数组末尾的元素移除 var removedFromMyArray=myArray.pop() 将myArray数组的最后一个元素移除并 ...
- Linux rar
http://www.vpsyou.com/2010/06/15/to-extract-rar-centos.html wget http://www.rarsoft.com/rar/rarlinux ...
- php header头信息 举例
发布:sunday01 来源:Net [大 中 小] 转自:http://www.jbxue.com/article/6915.html 用于演示PHP header()函数用法的代码,介 ...
- PHP MYSQL 数据库配置连接
//配置文件 Config.php // 默认使用数据库配置 ***************************************** $Config['ConnectTag'] = 'de ...
- Python变量类型(l整型,长整形,浮点型,复数,列表,元组,字典)学习
#coding=utf-8 __author__ = 'Administrator' #Python变量类型 #Python数字,python支持四种不同的数据类型 int整型 long长整型 flo ...
- spark(一) build
(1)编译前的准备工作,安装jdk,解压maven,解压spark,解压scala并配置相关的环境变量 export JAVA_HOME=/opt/module/jdk1.6.0_45 export ...