Hibernate Update Query issue

女生的网名这么多〃 提交于 2019-12-06 04:38:52

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

Hibernate saveorupdate will take table entity ,I mean Pojo class as a parameter .Hibernate will recognize that entity in table by its primary key i.e id .

So you need not to add any other where clauses on with remaining parameters ..because you are already decided to save the entity regardless of data inside it .

If you really want to check the other parameter you have to check the same in java it self before Saveorupdate.

If you decided to save the whole Object data then and then only use saveOrUpdate other wise go for HQL or Criterias for more Restrictions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!