问题描述
嘿,我正在开发一个使用Spring和Hibernate的桌面应用程序,并且我有一个懒惰启动的问题。我查看了网页,并且每个解决方案都与查看模式中的打开会话相关,但我无法使用此模式。我也尝试从 HibernateTemplate
中获取 sessionfactory
,但它向我返回一个断开连接的会话。有没有人知道其他的解决方案?
-
在访问延迟初始化对象或集合时,让Hibernate会话保持打开状态。这意味着您将不得不在代码中仔细标记事务边界,即open session in view模式。 Spring使这成为可能,但在桌面应用程序中,它不会像Web应用程序那样简单,事务边界更加明显。
关闭在Hibernate中持久化对象的所有惰性初始化。 选项2可能会导致大量不必要的数据库访问和选项1意味着你必须认真研究你的工作流和用例。 Make arrangements to keep a Hibernate session open when you access a lazy-initialized object or collection. That means you're going to have to carefully mark your transaction boundaries in your code, a la the "open session in view" pattern. Spring makes this possible, but in a desktop application it won't be as straightforward as a web application where the transaction boundaries are a little more obvious.
Turn off all the lazy-initialization for your persisted objects in Hibernate.
希望有帮助!
Hey I am developing an desktop application using Spring and Hibernate, and I have a problem with lazy initiation. I looked in the web and every solution is related to the open session in view pattern, but I can't use this pattern. I've also tried to get the sessionfactory
from the HibernateTemplate
, but it returns to me a disconnected session.
Does anyone know other solution?
I would suggest that you basically have two solutions:
Option 2 could lead to a lot of unnecessary database access, and option 1 means you have to seriously study your workflow and use cases.
Hope that helps!
这篇关于Spring和Hibernate,懒惰启动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!