MessageConverter 中带有 @Transactional 注释的 LazyInitializationException
我有一个通过 Spring-MVC 公开的 REST 服务。我有一个特定的方法,它被正确映射并通过 HTTP 调用进行调用。我的 Spring 应用程序包含 HibernateTransactionManager
,并且事务是通过 @Transactional
注释配置的。我这样注释该方法:
@Transactional(readOnly = true)
@Override
@RequestMapping(value = "/start", method = RequestMethod.GET)
@ResponseBody
public List<SomeObject> start(....)
每当我调用 HTTP 方法时,我都会从我的 org.springframework.http.converter.json.MappingJacksonHttpMessageConverter 中得到一个 org.hibernate.LazyInitializationException 异常,它是绑定在我的应用程序上下文中。 @Transactional
注释对于 MessageConverter
也有效吗?
I have a REST-service exposed through Spring-MVC. I have a particular method which is correctly mapped and called through a HTTP-call. My Spring application contains the HibernateTransactionManager
and transactions are configured through @Transactional
-annotations. I annotated the method like this:
@Transactional(readOnly = true)
@Override
@RequestMapping(value = "/start", method = RequestMethod.GET)
@ResponseBody
public List<SomeObject> start(....)
Whenever I call the HTTP-method I a org.hibernate.LazyInitializationException
from my org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
which is bound in my application context. Is the @Transactional
annotation valid for the MessageConverter
as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的转换器类显然正在读取一个配置为在 Hibernate 配置中延迟收集的字段。
两种可能的解决方案:
Your converter class is obviously reading a field that's configured to be collected lazily in your Hibernate configuration.
Two possible solutions:
LazyInitializationException
. (This field can for example be a part of a relationship between two tables in the database.)LazyInitializationException
意味着当您尝试读取实体上未初始化的数据时,您的休眠Session
已关闭。您可以通过
Hibernate.initialize(entity)
LazyInitializationException
means that your hibernateSession
is closed at the time you try to read uninitialized data on your entity.You can fix this by:
Hibernate.initialize(entity)