简体   繁体   English

Hibernate LazyInitializationException:无法初始化代理-没有会话

[英]Hibernate LazyInitializationException: could not initialize proxy - no Session

I have structure like this mapped in Hibernate (using XDoclet because it is old project): 我在Hibernate中映射了这样的结构(使用XDoclet,因为它是旧项目):

public class Student {
     String name;
     int age;
     School school;    //many to one
     // ...
}

public class School {
     String name;
     // ...
}

I would like to print student's school name (using student object), like this: 我想打印学生的学校名称(使用学生对象),如下所示:

System.out.println("School name: " + student.getSchool().getName());

but I am getting: 但我得到:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session org.hibernate.LazyInitializationException:无法初始化代理-没有会话

I wouldn't like to change mappings files (because I am not author of this), so what is other solution of this problem? 我不想更改映射文件(因为我不是这个的作者),那么该问题的其他解决方案是什么?

How did you read Student object? 您如何阅读学生对象?
Because if you are reading a Student from a method that close the session before returning, accessing a @Lazy collection will generate a LazyInitializationException . 因为如果您正在从返回之前关闭会话的方法中读取Student ,则访问@Lazy集合将生成LazyInitializationException
I suggest you to check your session lifecycle. 我建议您检查会话生命周期。

make sure your code calls the accessor methods of student before the session is closed. 在关闭会话之前,请确保您的代码调用了Student的accessor方法。 or in the case you are using spring and transactions, then it has to be under the scope where your session is active. 或者如果您使用的是Spring和Transaction,那么它必须在会话处于活动状态的范围内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate中的LazyInitializationException:无法初始化代理 - 没有Session - LazyInitializationException in Hibernate : could not initialize proxy - no Session 休眠问题:org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - Hibernate issue: org.hibernate.LazyInitializationException: could not initialize proxy - no Session LazyInitializationException-无法初始化代理-没有会话 - LazyInitializationException - could not initialize proxy - no Session LazyInitializationException:无法初始化代理 - 否 Session - LazyInitializationException: could not initialize proxy - no Session org.hibernate.LazyInitializationException:无法初始化代理-没有会话-很少发生 - org.hibernate.LazyInitializationException: could not initialize proxy - no Session - occurs rarely 如何修复 org.hibernate.LazyInitializationException - 无法初始化代理 - 没有 Session - How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session org.hibernate.LazyInitializationException:无法初始化proxy -no Session - org.hibernate.LazyInitializationException: could not initialize proxy -no Session org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话 - org.hibernate.LazyInitializationException: could not initialize proxy - no Session Spring JPA - org.hibernate.LazyInitializationException:无法初始化代理 - 无 Z71AB3B3AE294B3ABDE46 - Spring JPA - org.hibernate.LazyInitializationException: could not initialize proxy - no Session JPA存储库org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - JPA repository org.hibernate.LazyInitializationException: could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM