更新并选择一个Hibernate查询

时间:2017-10-19 18:58:11

标签: java sql 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计数是正确的,但是UPDATE查询未使用该值更新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'"

更新未对数据库采取任何操作。

有任何线索吗?

1 个答案:

答案 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();

   }