
[英]javax.persistence.TransactionRequiredException: No active transaction for PuId=
[英]javax.persistence.TransactionRequiredException: There is no currently active transaction
我有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);
}
}
在这一行上, oPhoneDao.merge(oPhone);
得到错误:
javax.persistence.TransactionRequiredException: There is no currently active transaction.
我的合并方法:
@Override
public E merge(E e) {
E merge = entityManager.merge(e);
entityManager.flush();
return merge;
}
和我的bean配置
<bean id="oPhoneBean" class="....services.OPhoneService" init-method="init"
scope="singleton">
<jpa:context unitname="ophone" property="entityManager"/>
<tx:transaction method="*" value="Required"/>
</bean>
您需要在merge方法中启动并提交事务。
@Override
public E merge(E e) {
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
E merge = entityManager.merge(e);
tx.commit();
entityManager.flush();
return merge;
}
这是白羊座蓝图中的一个已知问题。 事务拦截器未添加到init方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.