java - Hibernate Update Query issue -
java - Hibernate Update Query issue -
for update query
update testdb.dbo.myemp set empname=? empid=?
i wrote in dao class
myemployee myemployee = new myemployee(); myemployee myemployee =(myemployee )session.load(myemployee.class, new integer(1700)); myemployee.setname("updatedname"); session.update(myemployee );
its working fine, need know type of update query mentioned in below
update testdb.dbo.myemp set empsalary=? empid=? && empname = ?
(i.e., need update table using 2 conditions in clause , can done hql , want know how can implement using saveorupdate method..)
how can update using update or saveorupdate method()? whether possible in hibernate ?
you must fetch object first, in 2 ways:
1- hql
query query = session.createquery("from myemployee e e.id = ? , e.name = ?"); query.setparameter(0, 1); query.setparameter(1, "myname"); myemployee e = (myemployee) query.uniqueresult(); e.setsalary(5000); session.saveorupdate(e);
2- criteria
criteria criteria = session.createcriteria(myemployee.class); criteria.add(restrictionss.eq("id", 1)).add(restrictions.eq("name", "myname"); myemployee e = (myemployee) criteria.uniqueresult(); e.setsalary(5000); session.saveorupdate(e);
by way in default flush mode, when fetch object, , update it, persisted @ end of session automatically (if not using statelesssession).
java hibernate orm sql-update dao
Comments
Post a Comment