javajavax。坚持不懈TransactionRequiredException:当前没有活动事务
我使用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>
# 1 楼答案
这是白羊座蓝图中的一个已知问题。事务拦截器未添加到init方法
见ARIES-1715
# 2 楼答案
您需要在合并方法中启动并提交事务