blo*_*low 8 spring annotations hibernate transactions
我试图使声明式交易工作.
这是我的spring.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:h2:tcp://my/db/path" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="data" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<context:component-scan base-package="test" />
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
这是我的控制器实现:
//file TestController.java
public interface TestController {
public List<Test> findAll();
}
//file TestControllerImp.java
@Controller
public class TestControllerImp implements TestController{
@Autowired
private SessionFactory sessionFactory;
/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory=sessionFactory;
}
@Transactional
public List<Test> findAll() {
return sessionFactory.getCurrentSession().createQuery("from Test").list();
}
}
Run Code Online (Sandbox Code Playgroud)
两者都在名为test的内部包中.
这是我的尝试:
TestController tc=context.getBean(TestController.class);
List<Test> list=tc.findAll();
Run Code Online (Sandbox Code Playgroud)
但这引发了一个例外:
org.hibernate.HibernateException:没有活动事务,createQuery无效
为什么transactionManager不起作用?我希望@Transactional注释所有事务都将由spring框架管理.我能做什么?
谢谢大家.
axt*_*avt 20
删除以下行,当Spring管理事务时不需要它们:
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
Run Code Online (Sandbox Code Playgroud)
实际上,设置hibernate.current_session_context_class有效地禁用了Spring事务管理,请参阅AbstractSessionFactoryBean.setExposeTransactionAwareSessionFactory()javadoc:
关闭此标志以使用Hibernate的默认getCurrentSession()行为公开普通的Hibernate SessionFactory,仅支持纯JTA同步.或者,只需覆盖相应的Hibernate属性"hibernate.current_session_context_class".
| 归档时间: |
|
| 查看次数: |
13900 次 |
| 最近记录: |