2016-01-22 55 views
1

同樣的查詢在mysql workbrench工作正常,但是當我在程序中運行它沒有結果,甚至沒有錯誤。真的不知道如何解決這個問題。更新查詢,工作正常,但不是我島類

我使用休眠5.0.0和MySQL連接器5.0.5

道看起來像這樣:

Session session = null; 
    Transaction tx = null; 

    try { 
     session = HibernateAnnotationUtil.getSessionFactory().openSession(); 
     tx = session.beginTransaction(); 
     String hql = "UPDATE cms_asset SET `value` = CASE application_structure_id " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "WHEN (select id from application_structure where `name` = ? and cms_id = ?) THEN ? " 
       + "ELSE `value` END WHERE application_structure_id > 0;"; 
     Query query = session.createSQLQuery(hql); 
     query.setParameter(0, "asd"); 
     ... 
     query.setParameter(26, "asd2"); 
     Integer result = query.executeUpdate(); 
     tx.commit(); 
     if(result > 0){ 
      return true; 
     }else { 
      return false; 
     } 

    } catch (Exception e) { 
     try { 
      tx.rollback(); 
     } catch (RuntimeException rbe) { 
      rbe.printStackTrace(); 
     } 
     e.printStackTrace(); 
     return null; 
    } finally { 
     if (session != null) { 
      session.close(); 
     } 
    } 
+0

它完成了嗎?它運行,甚至?它會拋出異常嗎? (只要假設您遺漏的代碼不會削弱該操作)。你調試過了嗎? – Stultuske

+0

它工作正常,但沒有更改數據庫。我沒有堆棧跟蹤。此查詢中的結果是表中的行數。沒有例外,沒有任何東西 – Faron

回答

0

問題的解決方案是將引擎表從InnoDB更改爲MyISAM,然後更新正常。

0

至於我記得你已經使用SQLQuery接口來處理本地SQL更新。例如。

SQLQuery query = session.createSQLQuery(hql); 
    query.setParameter(0, "asd"); 
    ... 
    query.setParameter(26, "asd2"); 
    Integer result = query.executeUpdate(); 

否則,您可能需要使用Custom SQL Annotations。但HQL將是最好的選擇。