休眠更新查询问题 [英] Hibernate Update Query issue

查看:108
本文介绍了休眠更新查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此更新查询

update TestDB.dbo.MyEmp set empname=? where empid=?

我在我的DAO课上写了

I wrote in my 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, but I need to know for this type of update query mentioned in below

update TestDB.dbo.MyEmp set empsalary=? where empid=? && empname = ?

(即,我需要在where子句中使用两个条件来更新表,这可以由HQL完成,但我想知道我们如何通过使用saveOrUpdate方法来实现此功能.)

(i.e., I need to update the table by using two conditions in where clause , this can be done by HQL , but i want to know how can we implement this by using saveOrUpdate method..)

如何使用update或saveOrUpdate method()进行更新? 是否有可能在休眠状态下这样做?

How I can do update by using update or saveOrUpdate method()? Whether its possible to do like that in hibernate ?

推荐答案

必须首先通过两种方式获取对象:

You must fetch your object first, in two ways:

1- HQL

Query query = session.createQuery("from MyEmployee e where e.id = ? and e.name = ?");
query.setParameter(0, 1);
query.setParameter(1, "myName");
MyEmployee e = (MyEmployee) query.uniqueResult();
e.setSalary(5000);
session.saveOrUpdate(e);

2-条件

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);

在默认刷新模式下,当您获取对象并对其进行更新时,该对象将在会话结束时自动保留(如果您不使用StatelessSession).

By the way in default flush mode, when you fetch the object, and update it, it will be persisted at the end of session automatically (if you are not using StatelessSession).

这篇关于休眠更新查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆