通过连接更新值
使用 Hibernate,我想根据条件更新数据库中的数据,但出现以下错误:“要遍历的节点不能为空”
这是我的数据库描述:
Account: id, email, password
Member : id, account, team
Team: id, current (and a reference to member => members)
这是我的 JPA:
UPDATE Team t SET t.current = :current LEFT JOIN t.members m WHERE t.current = :current_true AND m.account = :account
我做错了什么? 如果我将 LEFT JOIN 移至 SET 之前:
UPDATE Team t LEFT JOIN t.members m SET t.current = :current WHERE t.current = :current_true AND m.account = :account
我得到:“期望 SET,找到 LEFT”
如果我删除联接:
UPDATE Team t SET t.current = :current WHERE t.current = :current_true AND t.members.account = :account
我得到:“非法尝试取消引用集合”。
更新值的正确方法是什么?
感谢您的帮助!
using Hibernate, I'd like to update a data in the database based on conditions, but I got the following error : "node to traverse cannot be null"
Here is my database description :
Account: id, email, password
Member : id, account, team
Team: id, current (and a reference to member => members)
Here is my JPA :
UPDATE Team t SET t.current = :current LEFT JOIN t.members m WHERE t.current = :current_true AND m.account = :account
What am I doing wrong?
If i move the LEFT JOIN to before the SET :
UPDATE Team t LEFT JOIN t.members m SET t.current = :current WHERE t.current = :current_true AND m.account = :account
I got : "expecting SET, found LEFT"
If I remove the join :
UPDATE Team t SET t.current = :current WHERE t.current = :current_true AND t.members.account = :account
I got : "Illegal attempt to dereference collection".
What is the correct way to update values ?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用子查询:(
未测试)
Use a subquery:
(not tested)
第 4 章中的 JPA 2.0 规范包含 JPQL 中所有支持的功能的详细信息。这是“update”语句的定义:
这些操作的语法如下:
正如您所看到的,这里没有说明对多个实体的支持。我想您将必须找到一种不同的方法来执行此操作,也许创建一个方法来首先选择要更新的实体,然后迭代设置值的结果。或者您可以使用本机 SQL 更新。
The JPA 2.0 specification in chapter 4 contains details of all supported features in JPQL. This is the definition of the "update" statement:
The syntax of these operations is as follows:
As you can see, support for multiple entities is not stated here. I guess you will have to find a different way to do it, perhaps create a method that selects the entities that you want to update first, and then iterate over the results setting the values. Or you could use a native SQL update.