通过连接更新值 [英] Update value with join
问题描述
使用Hibernate,我想根据条件更新数据库中的数据,但我得到以下错误:要遍历的节点不能为空
这是我的数据库描述:
账户:id,email,密码
成员:id,account,team
团队:id,current(以及对会员=>成员的引用)
我的JPA:
UPDATE Team t SET t.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
我得到了:expect SET,found LEFT
如果我删除了连接:
UPDATE Team t SET t.current =:current WHERE t.current =:current_true AND t.members.account =:account
我得到了:非法尝试取消引用collection。
更新值的正确方法是什么?
感谢您的帮助!
JPQL中的功能。这是update语句的定义:这些操作的语法如下:
update_statement :: = update_clause [where_clause]
update_clause :: = UPDATE entity_name [[AS] identification_variable]
SET update_item {,update_item} *
update_item: := [identification_variable。] {state_field | single_valued_object_field} =
new_value
new_value :: =
scalar_expression |
simple_entity_expression |
NULL
正如您所看到的,这里没有提到对多个实体的支持。我想你必须找到一种不同的方式来做到这一点,也许创建一个方法来选择你想要先更新的实体,然后迭代设置值的结果。或者您可以使用本地SQL更新。
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!
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:
update_statement ::= update_clause [where_clause]
update_clause ::= UPDATE entity_name [[AS] identification_variable]
SET update_item {, update_item}*
update_item ::= [identification_variable.]{state_field | single_valued_object_field} =
new_value
new_value ::=
scalar_expression |
simple_entity_expression |
NULL
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.
这篇关于通过连接更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!