主外键关联取出主键时报错[org.hibernate.LazyInitializationException] could not initialize proxy - no Session

网友投稿 975 2022-10-06

主外键关联取出主键时报错[org.hibernate.LazyInitializationException] could not initialize proxy - no Session

主外键关联取出主键时报错[org.hibernate.LazyInitializationException] could not initialize proxy - no Session

表结构设计 Users:

Types:

其中 Users 的 tid 为与 Types 的 id 关联的外键

测试:

@Test public void loginTest(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IUsersService usdo= (IUsersService) context.getBean("UsersService"); List li = usdo.findAll(); for(Users u:li){ System.out.println(u.getId() + " , "+u.getUsername() + " , " + u.getPassword() + " , " + u.getTypes().getPosition());

报错如下:

2016-04-20 12:24:55 ERROR [org.hibernate.LazyInitializationException] could not initialize proxy - no Sessionorg.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.test.entity.Types_$$_javassist_1.getPosition(Types_$$_javassist_1.java) at com.test.UsersTest.loginTest(UsersTest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 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:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

从报错可以看出,是延迟加载初始化,关联表默认延迟加载,即 lazy=”true”

将关联表的Types.hbm.xml文件中

部分添加 lazy=”false” 改为

运行结果:

1 , aaaaaa , aaa , manager2 , bbbbbb , bbb , service3 , cccccc , ccc , else4 , dddddd , ddd , service6

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于微信小程序在Chrome浏览器上运行以及WebStorm的使用
下一篇:微信小程序开发圆形菜单(仿建行圆形菜单)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~