2017-04-27 53 views
0

我服務與的EntityManager並在init()方法創建DAO類,並傳遞到的EntityManager DAO consctructor:javax.persistence.TransactionRequiredException:當前沒有活動的事務

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

回答

0

您需要在合併方法中啓動並提交事務。

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

這是白羊座藍圖中的一個已知問題。事務攔截器不會添加到init方法。

請參閱ARIES-1715

+0

好的。它用於測試。我用init方法寫這個,因爲當我使用石英方法時,我得到一些錯誤。我在init方法中創建dao,然後嘗試在調度程序quartz方法中使用此dao,當我調用合併時,我的應用程序凍結 – user5620472

+0

我很高興我退出並且不再與karaf一起工作。 – user5620472

+0

那你爲什麼問? –

相關問題