2017-10-19 88 views
0

我想在Hibernate中,看起來要執行一個查詢像更新並選擇一個Hibernate查詢,

private void setFailedLogins(String user_id) throws Exception { 

     this.openDBTransaction(); 
     Query query = session.createQuery(
      "UPDATE User SET loginDate= CURRENT_TIMESTAMP, loginAttempts=(SELECT COUNT(*) FROM loginHistory WHERE user_id=:user_id AND response!=:response AND TIMESTAMPDIFF(MINUTE,valTime,CURRENT_TIMESTAMP) <= 30*24*60) WHERE user_id=:user_id" 
       ); 
      query.setParameter("user_id", user_id); 
      query.setParameter("response", "OK"); 
      int result = query.executeUpdate(); 
     this.closeDBTransaction(); 
    } 

loginAttempts計數是正確的,但隨後更新查詢未與值

其他更新loginAttempts關於Hibernate的打擊信息

Hibernate屬性

<property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop> 
      <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop> 
      <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop> 
      <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop> 
      <prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}</prop> 

      <!-- Show and print nice SQL on stdout --> 
      <prop key="show_sql">${hibernate.show_sql}</prop> 
      <prop key="format_sql">${hibernate.format_sql}</prop> 
      <prop key="use_sql_comments">${hibernate.use_sql_comments}</prop> 
      <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
      <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</prop> 
      <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> 
     </props> 
    </property> 

hibernate.hib_domain_package=com.retro.app.domain 
hibernate.connection.driver_class = com.mysql.jdbc.Driver 
hibernate.connection.url = jdbc:mysql://localhost:3306/mydb?zeroDateTimeBehavior=convertToNull 
hibernate.connection.username = ****** 
hibernate.connection.password = ****** 
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 
hibernate.c3p0.min_size=8 
hibernate.c3p0.max_size=20 
hibernate.c3p0.timeout=300 
hibernate.c3p0.max_statements=40 
hibernate.c3p0.idle_test_period=3000 
hibernate.show_sql=false 
hibernate.format_sql=false 
hibernate.use_sql_comments=false 
hibernate.hbm2ddl.auto=create 

附註:當我更改查詢到喜歡的東西很簡單:

"UPDATE User SET loginDate= CURRENT_TIMESTAMP, loginAttempts=66 WHERE user_id='USER_3000'" 

更新不採取任何行動的數據庫上。

任何線索?

+0

你有一個事務打開?如果是,請檢查Session.commit()是否爲呼叫。否則,驗證AutoCommit屬性是如何設置的,如果我沒有錯,hibernate的默認策略是false。 –

+0

@BaptisteBeauvais我不確定,因爲我是Hibernate的新手。認爲它會很容易,因爲它使用PHP中的PDO – JackTheKnife

+0

它並不是真的更復雜,你可以提供你的休眠配置和你請求查詢的完整方法,看看這是否證實了我的想法。 –

回答

0

好的。得到它解決,但沒有線索是什麼不同。下面

解決方案:

@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED) 
private void setFailedCount(String user_id) throws Exception { 

     Session session = sessionFactory.openSession(); 
     Transaction tx = session.beginTransaction(); 
     String hqlUpdate = "UPDATE User SET loginDate= CURRENT_TIMESTAMP, loginAttempts=(SELECT COUNT(*) FROM loginHistory WHERE user_id=:user_id AND response!=:response AND TIMESTAMPDIFF(MINUTE,valTime,CURRENT_TIMESTAMP) <= 30*24*60) WHERE user_id=:user_id"; 
     int updatedEntities = session.createQuery(hqlUpdate) 
       .setParameter("user_id", user_id) 
       .setParameter("response", "OK") 
       .executeUpdate(); 
     tx.commit(); 
     session.close(); 

    }