3

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
matrix
  • 53
  • 2
  • 6
  • Have you tried something yourself? Show your efforts, and show us what goes wrong, and we might be able to help you. – mthmulders Apr 13 '13 at 09:51
  • sir List l1 =getHibernateTemplate().find("from Login "); this will return all the data i want to know how to use this update Login set empSmartId = 48750005" +" where empPassword = 6328ef1675ddb7106eba8dc2661961d7" – matrix Apr 13 '13 at 09:52
  • This line of code doesn't do anything related to updating. I've down-voted because you've shown zero evidence of prior research and effort on your part. – mthmulders Apr 13 '13 at 09:53
  • Login login = new Login(); String hql = "UPDATE Login set empPassword = :encr " + "WHERE empSmartId = :smartid"; earlier i used this but i want to know how to use update using hibernate template HttpSession session = (HttpSession) getSession(true); Query query = ((Session) session).createQuery(hql); query.setParameter("encr", encr); query.setParameter("smartid", smartid); int result = query.executeUpdate();*/ – matrix Apr 13 '13 at 09:56
  • Do you get any error message, stacktrace or whatsoever when you run this code? – mthmulders Apr 13 '13 at 09:58
  • 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 – matrix Apr 13 '13 at 10:00
  • It's better to update your question instead of adding comments. – mthmulders Apr 13 '13 at 10:01

2 Answers2

0

you would be looking something like this

public class LoginDaoImp extends HibernateDaoSupport implements LoginDao 
{ 
    public boolean resetAttempt(Login login) 
    { 
        try 
        { 
            // you should create method for login to retrived based on password
            //Remember getting login  by password is not correct way, Instead you you should use primary key
            //Getting persisted object of Login
            Login persisted_login = getLoginByPassword(6328ef1675ddb7106eba8dc2661961d7);

            //Setting value in persisted object of Login
            persisted_login.setEmpSmartId (48750005); 
            getHibernateTemplate().update(persisted_login); 
            return true; 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return false; 
    }
}
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
0

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.

OO7
  • 2,785
  • 1
  • 21
  • 33