I'm trying to execute one query in Hibernate which looks like
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
count is correct but then UPDATE query is not updating loginAttempts
with that value
Additional info regarding Hibernate blow
Hibernate properties
<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
Side note: When I change query to something simple like:
"UPDATE User SET loginDate= CURRENT_TIMESTAMP, loginAttempts=66 WHERE user_id='USER_3000'"
Update is not taking any action on a database.
Any clue?
OK. Got it solved but have no clue what is different.
Solution below:
@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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.