使用Hibernate project.DispatcherServlet和ContextLoaderListener在Spring MVC中配置web.xml,每个都有一个sessionFactory [英] Configuring web.xml in a Spring MVC with Hibernate project.DispatcherServlet and ContextLoaderListener have each a sessionFactory
问题描述
就像标题所说的那样,即将配置一个带有Hibernate / JPA的Spring MVC项目来实现持久性。
Like the title says, am about to configure a Spring MVC project with Hibernate/JPA for persistence.
我记得我为<$ c使用了相同的上下文$ c> DispatcherServlet 和 ContextLoaderListener
直到最近我才被建议将它们分开。但在分离时我发现两者都加载 SessionFactory
使我的 OpenSessionInViewFilter
痛苦然后我就是将关注点分开,只将MVC问题留给 DispatcherServlet
。
I remember I used the same context for both DispatcherServlet
and ContextLoaderListener
until recently I've being advised to separate them. But in separating the I've found out that both were loading a SessionFactory
making my OpenSessionInViewFilter
a pain then I've separated the concerns, leaving only MVC concerns to the DispatcherServlet
.
除了在需要时有一个加载集合的机制,在调用这个父对象时,有哪些其他提示可以避免臭名昭着的 LazyInitializationException
?
Aside having a mechanism to load collections when needed, when calling this parent objects, what are the other tips to avoid the infamous LazyInitializationException
?
推荐答案
如果你的工作单元无法按请求自动生成,我认为您可以在服务中手动创建它使用事务的图层。这样的事情:
If your "unit of work" cannot be automatically per request, I think you can create it manually in your service layer using a transaction. Something like this:
public Object serviceMethod(params) {
TransactionTemplate transactionTemplate;
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
try {
// call your DAO's to update/delete/... and set values to service
} catch (DAOException e) {
LOGGER.error(e);
throw new ServiceException(e);
}
}
});
}
这篇关于使用Hibernate project.DispatcherServlet和ContextLoaderListener在Spring MVC中配置web.xml,每个都有一个sessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!