【发布时间】:2012-03-23 18:28:10
【问题描述】:
使用 Spring 3.1,Mojarra,休眠
applicationContext.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
...
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:component-scan base-package="mypackage"/>
TestBean.java
@Component
@Scope("session")
public class TestBean {
@Autowired private @Getter @Setter HibernateTemplate hibernateTemplate=null;
public String submit(){
try{
this.test();
}catch (RuntimeException ex) {
FacesUtil.addWarn("Error");
}
return null;
}
@Transactional
public String test() {
Device d1=new Device();
hibernateTemplate.persist(d1);
if(1==1)
throw new RuntimeException("Testing");
Device d2=new Device();
hibernateTemplate.persist(d2);
return null;
}
}
这有效(回滚),但在浏览器中显示异常
<h:commandButton value="Submit" action="#{testBean.test}"/>
试图显示面孔消息,但这会提交 d1
<h:commandButton value="Submit" action="#{testBean.submit}"/>
调用其他 bean 的 (DAO) 事务方法也可以,但我希望在托管 bean 本身中有代码。我应该如何在 JSF 中处理事务?
【问题讨论】:
标签: spring hibernate jsf-2 spring-transactions