gpt4 book ai didi

java - Hibernate 模板更新查询

转载 作者:行者123 更新时间:2023-12-01 04:45:06 25 4
gpt4 key购买 nike

如何使用hibernate模板使用hql更新查询,这是hql语句“update Login set empSmartId = 48750005”+“where empPassword = 6328ef1675ddb7106eba8dc2661961d7”

使用 getHibernatetemplate()

当前代码:

public class LoginDaoImp extends HibernateDaoSupport implements LoginDao { 
public boolean resetAttempt(Login login) {
try {
login.setAttempt(0);
getHibernateTemplate().update(login);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

}

我可以在代码工作之上保存整个pojo类,但我想使用where条件

最佳答案

HibernateDaoSupport 将有一个 getSession() 方法,它将返回配置的 DataSource 的 hibernate session 对象。使用此 session ,您可以说

Query updateQuery = getSession().createQuery("update Login l set l.empSmartId = :smartId where password = :password")
.setParameter("smartId", 48750005)
.setParameter("password", "6328ef1675ddb7106eba8dc2661961d7");
int noOfUpdatedRows = updateQuery.executeUpdate();

关于java - Hibernate 模板更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com