【问题标题】:@Transactional not working with spring and hibernate@Transactional 不适用于 spring 和 hibernate
【发布时间】:2011-01-22 06:00:50
【问题描述】:

我正在尝试与@Transactional 进行春季交易,但没有任何成功。

摘自我的 applicationContext.xml:

<context:annotation-config />
<context:component-scan base-package="hibex" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="org.postgresql.Driver" p:url="jdbc:postgresql://localhost:5432/hibex"
    p:username="clubspace" p:password="clubspace" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/hibernate.cfg.xml" />
    <property name="entityInterceptor" ref="beanValidationEventListener" />
</bean>

<!-- a PlatformTransactionManager is still required -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <!--  org.springframework.transaction.jta.JtaTransactionManager
    org.springframework.jdbc.datasource.DataSourceTransactionManager -->
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven />

<aop:aspectj-autoproxy />

我的完整 spring 配置文件是 here

还有我的方法,其中事务不起作用:

@Transactional
public void m2() {
    OwnerBO owner2 = ownerManager.get(owner.getId());
    owner2.getPets().add(new PetBO("Ubul"));
}

这会导致:

1375 [main] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: hibex.bo.OwnerBO.pets, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: hibex.bo.OwnerBO.pets, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
    at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212)
    at hibex.code.Service.m2(Service.java:52)
    at hibex.code.App.run(App.java:15)
    at hibex.code.Main.main(Main.java:14)
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: hibex.bo.OwnerBO.pets, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
    at org.hibernate.collection.PersistentSet.add(PersistentSet.java:212)
    at hibex.code.Service.m2(Service.java:52)
    at hibex.code.App.run(App.java:15)
    at hibex.code.Main.main(Main.java:14)

从这个类调用服务:

@Component
public class App {
    @Autowired
    private Service service;
    public void setService(Service service) {
        this.service = service;
    }
    public void run() {
        service.m2();
    }
}

有什么想法吗?

GenericManager 在这里:GenericManager on Pastebin

get 方法:

public T get(PK id) {
    return dao.findById(id);
}

还有GenericDaoImpl#findById(PK)

public E findById(PK id) {
    return getHibernateTemplate().get(getEntityClass(), id);
}

谢谢

变更日志:刚刚添加了附加信息(必要的代码 sn-ps)

【问题讨论】:

  • 你能给我看看 OwnerManager.get() 的代码吗(我怀疑这是在你的通用模板中)。问题不在于事务,而在于当您不希望休眠会话关闭时
  • 我修改帖子暴露get()

标签: hibernate spring transactions annotations


【解决方案1】:

该异常与 Hibernate 延迟加载支持有关。 当您调用 owner2.getPets().add() 时,Hibernate 会话似乎未打开。 难道是 ownerManager.get() 正在关闭 Session 吗?

【讨论】:

  • 我修改了帖子以包含get() 方法
  • "延迟加载也只适用于打开的 Hibernate 会话,无论是在事务中还是在 OpenSessionInViewFilter/Interceptor 中。" 请参阅 static.springsource.org/spring/docs/1.2.9/api/org/…
  • 如果 OwnerManager#get 方法尚未注解,您可能需要使用 @Transactional 进行注解。
【解决方案2】:

HibernateTransactionManager 应与SessionFactory 一起提供,请参阅javadoc

【讨论】:

  • 那么你说我的transactionManager bean 中应该有&lt;property name="sessionFactory" ref="sessionFactory" /&gt; 吗?
  • 啊,谢谢,这对我有帮助(赞成),但不是解决我问题的最终解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 2020-10-25
  • 2011-05-11
  • 1970-01-01
相关资源
最近更新 更多