javax.persistence.TransactionRequiredException:当前没有活动的事务

时间:2017-04-27 10:03:30

标签: java hibernate jpa osgi entity

我使用EntityManager服务并在init方法中创建DAO类并将EntityManager传递给DAO consctructor。:

@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>

2 个答案:

答案 0 :(得分:0)

您需要在合并方法中启动并提交事务。

@Override
public E merge(E e) {
    EntityTransaction tx = entityManager.getTransaction();
    tx.begin();
    E merge = entityManager.merge(e);
    tx.commit();
    entityManager.flush();
    return merge;
}

答案 1 :(得分:0)

这是Aries蓝图中的一个已知问题。事务拦截器未添加到init方法。

请参阅ARIES-1715