2015-10-20 39 views
0

我在spring context.xml中有下面的代碼。獲取錯誤執行更新/刪除。即使有q.setLockMode(LockModeType.PESSIMISTIC_WRITE);或沒有這個。可能是我錯過了一些配置,以更新表javax.persistence.TransactionRequiredException:執行更新/刪除queryAbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96)

@Override 
    @Transactional(propagation = Propagation.REQUIRED) 
    public Boolean updateXXXX(final EO workflowHistory) { 
     Boolean updateFlag=false; 
     String que = "update table \n" + 
        "set status =:statusname , \n" + 
        "actor =:actor \n" + 
        "where ID = (\n" + 
        "select MAX(id) from table \n" + 
        "where \n" + 
        "id =:processInstanceID)"; 

     try{ 
      final Query q = entityManager.createNativeQuery(que); 
      System.out.println("11 query...."+q.getLockMode()); 
      q.setParameter("actor", workflowHistory.getActor()); 
      q.setParameter("statusname", workflowHistory.getStatus()); 
      q.setParameter("processInstanceID", workflowHistory.getProcessinstanceid()); 

      q.executeUpdate(); 

     } catch(Exception e){ 
      System.out.println("In updateWorkflowHistory...."); 
      e.printStackTrace(); 
     } 
     updateFlag=true; 
     return updateFlag; 
    } 
+0

如果你有Hibernate,那麼你爲什麼要編寫SQL查詢並執行它們。使用session.merge(updated_class_object);這將更新數據庫中的現有行。 –

+0

我需要「從表中選擇MAX(id)\ n」+ 「其中\ n」+ 「id =:processInstanceID」值,所以我不想將2個事務處理爲數據庫 – Viji

回答

0

添加下面的配置到你Spring上下文XML
TX:註解驅動的事務管理器=「transactionManager的」
將解決問題