我正在尝试与@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 />
我的完整弹簧配置文件是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);
}
由于
更改日志:刚刚添加了其他信息(必要的代码段)
答案 0 :(得分:2)
HibernateTransactionManager
应与SessionFactory
一起提供,请参阅javadoc。
答案 1 :(得分:0)
该异常与Hibernate延迟加载支持有关。 当你调用owner2.getPets()。add()时,似乎没有打开Hibernate会话。 可能是ownerManager.get()正在关闭会话吗?