我正在使用以下方法来更新数据库中的数据。
String hql = "UPDATE EmployeeSalary set salary = :sl,"
+ "monthYr=:dt "
+ "WHERE id =:id and client.id=:cid";
for (EmployeeSalary e : eList) {
Query query = session.createQuery(hql);
query.setParameter("sl", e.getSalary());
query.setParameter("dt", e.getMonthYr());
query.setParameter("id", e.getId());
query.setParameter("cid", e.getClient().getId());
int result = query.executeUpdate();
System.out.println("result is " + result);
if (eAttList.size() % 20 == 0) {
session.flush();
session.clear();
}
}
代码有问题吗
执行时显示
结果是0
如何解决上述问题。
最佳答案
The documentation关于更新查询说:
No "Forms of join syntax", either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.
您的查询似乎违反了此规则:client.id=:cid
是对客户端实体的隐式联接。
请注意,您正在让自己的生活变得困难。您可以简单地通过 ID 从 session 中获取实体(使用 Session.get()),然后更新它。更新查询对于一次更新多行很有用。
关于java - hibernate 更新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25718746/