HibernateTemplate更新查询 [英] HibernateTemplate Update Query
问题描述
如何使用hql更新查询使用hibernate模板
是hql语句update login set empSmartId = 48750005+where empPassword = 6328ef1675ddb7106eba8dc2661961d7
使用getHibernatetemplate()
当前代码:
public class LoginDaoImp扩展HibernateDaoSupport实现LoginDao {
public boolean resetAttempt(Login login){
try {login.setAttempt(0);
getHibernateTemplate()。update(login);
返回true;
} catch(Exception e){e.printStackTrace(); }
返回false; }
i可以将整个pojo类保存在代码上方,但是我想使用where条件来只更新hibernateTemplate来更新数据
我知道这个问题很老,但是,这个解决方案可能会帮助其他人......
以下是更新数据库记录的技巧。
关键是首先从数据库中获取所需的记录,使用关联的POJO类的设置器方法更新所需的字段&然后调用 hibernateTemplate.save(Object entity)
方法,如下所示: - $ /
public void updateUser(User user,String password){
@SuppressWarnings(unchecked)
List< User> results =(List< User>)hibernateTemplate
.find(FROM User where username =?,new Object [] {new String(user.getUsername())});
User userToBeUpdate = results.get(0);
userToBeUpdate.setPassword(password);
hibernateTemplate.save(userToBeUpdate);
}
这是我工作的解决方案。
How to use hql update query using hibernate template thi is the hql statement "update Login set empSmartId = 48750005" +" where empPassword = 6328ef1675ddb7106eba8dc2661961d7"
using getHibernatetemplate()
Current code:
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; }
i can save whole pojo class above code work but i want to use where condition to update only hibernateTemplate to update the data
I know this question is very old but, may this solution help someone else...
Here is a trick to update a record in DB.
The trick is first fetch the required record from the DB, update the required fields using setter methods of associated POJO class & then give a call to hibernateTemplate.save(Object entity)
method, just like below:-
public void updateUser(User user, String password) {
@SuppressWarnings("unchecked")
List<User> results = (List<User>) hibernateTemplate
.find("FROM User where username = ?", new Object[] { new String(user.getUsername()) });
User userToBeUpdate = results.get(0);
userToBeUpdate.setPassword(password);
hibernateTemplate.save(userToBeUpdate);
}
This is working solution in my case.
这篇关于HibernateTemplate更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!