我只是一个 spring + hibernate 初学者。我正在做一个简单的项目,我设置了事务的所有内容,一个简单的服务,一个简单的 dao 和所有带注释的 bean。
使用列表作品,我可以使用条件查询列出对象及其属性。但是如果我尝试加载一个对象:
Session sess = sessionFactory.getCurrentSession();
Ordine res = (Ordine) sess.load(Ordine.class, id);
return res;
查询似乎工作正常,但当 View 尝试读取属性(字符串)时,我遇到了一个令人讨厌的异常:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
我什至注意到这是启动日志:
INFO: Bean 'mySessionFactory' of type [class org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
这一切意味着什么?
最佳答案
在我看来,你有几个选择
- 强制您的对象不被延迟加载(例如,在持久性注释上设置
fetch=FetchType.EAGER
)。 - OpenSessionInViewFilter
- 在 View 中调用它们之前,先调用数据层中例程的方法(以加载值)。
[编辑]这似乎根本不像我最初建议的那样有效。
- 添加transactions 。使用@Transactional 注释您的 Controller 例程。
关于java - Spring hibernate关于延迟加载的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4853976/