[英]javax.persistence.TransactionRequiredException: There is no currently active transaction
I have Service with EntityManager and create DAO class in init method and pass EntityManager to DAO consctructor.: 我有EntityManager服务,并在init方法中创建了DAO类,然后将EntityManager传递给DAO构造器。
@Slf4j
public class OPhoneService {
@Setter
private EntityManager entityManager;
public void init() {
log.info("init");
log.info(Thread.currentThread().getName());
oPhoneDao = new OPhoneDaoImpl(entityManager);
List<OPhone> oPhones = oPhoneDao.getAllOPhones(0);
OPhone oPhone = oPhones.get(0);
oPhone.setState(1);
oPhoneDao.merge(oPhone);
}
}
and on this line oPhoneDao.merge(oPhone);
在这一行上,
oPhoneDao.merge(oPhone);
get error: 得到错误:
javax.persistence.TransactionRequiredException: There is no currently active transaction.
my merge method: 我的合并方法:
@Override
public E merge(E e) {
E merge = entityManager.merge(e);
entityManager.flush();
return merge;
}
And my bean config 和我的bean配置
<bean id="oPhoneBean" class="....services.OPhoneService" init-method="init"
scope="singleton">
<jpa:context unitname="ophone" property="entityManager"/>
<tx:transaction method="*" value="Required"/>
</bean>
You need to start and commit transaction in your merge method. 您需要在merge方法中启动并提交事务。
@Override
public E merge(E e) {
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
E merge = entityManager.merge(e);
tx.commit();
entityManager.flush();
return merge;
}
This is a known issue in Aries blueprint. 这是白羊座蓝图中的一个已知问题。 The transactional interceptor is not added to init methods.
事务拦截器未添加到init方法中。
See ARIES-1715 见ARIES-1715
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.