@事务性不使用spring和hibernate

@事务性不使用spring和hibernate,hibernate,spring,transactions,annotations,Hibernate,Spring,Transactions,Annotations,我尝试使用@Transactional执行spring事务,但没有成功 摘录自my applicationContext.xml: <context:annotation-config /> <context:component-scan base-package="hibex" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSour

我尝试使用
@Transactional
执行spring事务,但没有成功

摘录自my 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 />
这导致:

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位于此处:

获取方法:

public T get(PK id) {
    return dao.findById(id);
}
以及
GenericDaoImpl#findById(PK)

谢谢


Changelog:刚刚添加了额外的信息(必要的代码片段)

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

HibernateTransactionManager
应随SessionFactory一起提供,请参见。

能否显示ownerManager.get()的代码(我怀疑这在您的通用模板中)。问题不在于事务,而在于Hibernate会话在您不想让它打开时被关闭我修改了post以公开get(),我修改了post以包含
get()
方法“延迟加载也只能在事务或OpenSessionInViewFilter/Interceptor中使用开放的Hibernate会话。“看,如果@Transactional还没有注释,你可能想用@Transactional注释OwnerManager#get方法。那么你是说,我应该在我的
transactionManager
bean中添加
?啊,谢谢,这帮了我的忙(upvote),但不是我问题的最终解决方案
public T get(PK id) {
    return dao.findById(id);
}
public E findById(PK id) {
    return getHibernateTemplate().get(getEntityClass(), id);
}