【问题标题】:Configuring web.xml in a Spring MVC with Hibernate project.DispatcherServlet and ContextLoaderListener have each a sessionFactory在带有 Hibernate 项目的 Spring MVC 中配置 web.xml。DispatcherServlet 和 ContextLoaderListener 各有一个 sessionFactory
【发布时间】:2012-09-01 02:03:12
【问题描述】:

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

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

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

【问题讨论】:

    标签: spring hibernate jakarta-ee spring-mvc web.xml


    【解决方案1】:

    如果您的“unit of work”不能根据请求自动生成,我认为您可以使用事务在服务层中手动创建它。像这样的:

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

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2015-12-31
      • 1970-01-01
      • 2012-06-16
      • 2017-08-30
      • 1970-01-01
      相关资源
      最近更新 更多