1

我无法确定在 jsf - spring - hibernate 情况下管理事务的最佳方法。让我想到的是我收到以下错误消息:

failed to lazily initialize a collection of role: [class name], no session or session was closed

我的托管 bean 称为 spring bean,它调用了使用休眠的 dao 并检索了具有一对多关系的集合。当我试图检查集合时,我得到了错误(我知道如果我遍历 dao 中的集合,这将保留数据,但我想超越这一点)。

这是托管 bean 代码:

@Transactional
private void loadAppointments() {

  employees = userService.getEmployees();

}

(注意:当我尝试遍历员工时,我得到了错误。)

这是spring bean调用:

public Set<Employee> getEmployees() {
    return getUserDAO().getEmployees();
}

这是 dao 代码:

List<Employee> employees = getSessionFactory().getCurrentSession().createQuery(" from Employee ").list();
return new HashSet<Employee>(employees);

这是我的与交易有关的 applicationContext.xml 的一部分:

<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>

<!-- Transaction Manager is defined -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="SessionFactory"/>
</bean>

我不确定为什么 @Transactional 注释在 Spring bean 中失败,有人可以向我解释一下吗?

一个人推荐使用 JPATransactionManager,但我没有这方面的经验,我想知道是否有其他人有任何建议。从我的 Spring bean 控制事务的最佳/最简单的方法是什么?是否有文章或其他内容列出了这些选项?这似乎是一个相当密集的主题,因此所有帮助表示赞赏。

编辑:com.dave.model.User com.dave.model.Client com.dave.model.Appointment com.dave.model.Employee com.dave.model.Certificate org.hibernate.dialect.MySQLDialect true

4

0 回答 0