SRy SRy - 3 months ago 37
SQL Question

How can I update specific field in database by HibernateTemplate

I want to Update a specific field in the database

Persons
table by using HibernateTemplate.I am trying to do like this but this not working.

public void updateDate(int Id,Date recievedDate){
Id=10;
recievedDate=2012-11-12;
String qureyString="update Persons set recievedDate=? where Id=? ";
getHibernateTemplate().update(qureyString, new Object[]{Id,recievedDate});
}


I am getting
UnkownEntity Exception
when I run this query.Can I do Update of a specific field at all by using HibernateTemplate? Is there any other alternate to do Speicific field update?

Answer

update method in getHibernateTemplate does not allow hql query to execute. It only allows hibernate entity object.

See link hibernate template update method

In your case Hibernate tries to resolve update Persons set recievedDate=? where Id=? as an entity.

Solution:

Query q = s.createQuery("update Persons set recievedDate=:recievedDate where Id=:Id");
q.setString("recievedDate", "some date");
q.setString("Id", "54");
q.executeUpdate();

Hope its clear.