1

就像标题说的那样,我即将使用 Hibernate/JPA 配置一个 Spring MVC 项目以实现持久性。

我记得我对两者都使用了相同的上下文DispatcherServletContextLoaderListener直到最近我才被建议将它们分开。但是在SessionFactory分离. OpenSessionInViewFilterDispatcherServlet

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

4

1 回答 1

0

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

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);
         }
    }
});
}
于 2012-09-07T10:17:01.547 回答