问题描述
我的Webapp中有以下JSF支持bean
I had the following JSF backing bean in my Webapp
@ManagedBean
class MyBackingBean implements Serializable {
private MyHibernateRepository repository;
...
@Transactional
public void save() {
....
repository.save(myObject);
}
}
在调用repository.save
方法时-出现以下错误
When it gets to the repository.save
method call - I get the following error
no transaction is in progress
我有两个问题
- 这是否是由于这样的错误引起的?
- 我相信有两种解决方法-还有其他解决方法吗?
- Is this because of a bug like this?
- I believe there are two workarounds - are there any others?
2.1第一种解决方法-使用
2.1 First workaround - using
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
repository.save(myObject);
}
});
2.2第二种解决方法
2.2 Second workaround
创建一个帮助器类并对其进行注释.
Create a helper class and annotate that instead.
2.3(第三个可能的解决方法是在内部类的方法上注释@Transactional,这与2.2非常相似).
2.3 (A possible third workaround would be to annotate @Transactional on a method of an inner class This is quite similar to 2.2).
推荐答案
在使用Spring注释时(我知道@Transactional
是Sun标准-但您需要实现)-Spring使用AOP对该类进行注释以添加交易处理代码.这仅适用于Spring bean.如果您的类是JSF的支持bean,则Mojarra框架不会在此注释中插入其自己的事务处理代码.
When using Spring annotations (I know that @Transactional
is a Sun standard - but you need an implementation) - Spring uses AOP to annotate to the class to add the transaction handling code. This only works for Spring beans. If your class is a backing bean for JSF - the Mojarra framework won't insert its own transaction handling code to this annotation.
简短答案-@Transactional
适用于Spring加载的bean.否则,您需要找到一个支持它的框架,或者假定它不起作用.
Short answer - @Transactional
works for beans loaded by Spring. Otherwise you need to find a framework that supports it or assume it won't work.
这篇关于@ManagedBean和@Transactional-Spring中的bug?解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!