2017-10-19 258 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(); 

    }