提问者:小点点

使用Hibernate project. DispatcherServlet和ContextLoaderListener在SpringMVC中配置web.xml


正如标题所说,我即将使用Hibernate/JPA配置一个SpringMVC项目以实现持久性。

我记得我对DispatcherServletContextLoaderListener使用了相同的上下文,直到最近我才被建议将它们分开。但是在分离时,我发现两者都在加载SessionFactory,这使得我的OpenSessionInViewFilter很痛苦,然后我分离了关注点,只给DispatcherServlet留下了MVC的关注点。

除了在需要时加载集合的机制之外,在调用此父对象时,还有哪些其他技巧可以避免臭名昭著的LazyFirst alizationException


共1个答案

匿名用户

如果您的“工作单元”不能根据请求自动执行,我认为您可以使用事务在服务层手动创建它。类似于这样:

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);
         }
    }
});
}