Jpa ElementCollection字段上的JPQL查询批量更新集

Jpa ElementCollection字段上的JPQL查询批量更新集,jpa,jpql,bulkupdate,Jpa,Jpql,Bulkupdate,我要更新以下JPA实体: @Entity(name = "EmployeeImpl") @Table(name = "EmployeeImpl") public class EmployeeImpl { @Id @Column(name = "employeeId") @GeneratedValue(strategy = GenerationType.AUTO) private long id; @ElementCollection private List<St

我要更新以下JPA实体:

@Entity(name = "EmployeeImpl")
@Table(name = "EmployeeImpl")
public class EmployeeImpl {
  @Id
  @Column(name = "employeeId")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  @ElementCollection
  private List<String> phonenumber;
}
但这不起作用:
异常描述:编译查询[updateEmployee:Update EmployeeImpl e SET e.phonenumber=:number,其中e.id=:id],第1行第28列:对SET子句目标[e]中的属性[phonenumber]的访问无效,SET子句中只能更新状态字段和单值关联字段。


问题是,如何更新
@ElementCollection
?如果可能的话,我想用一个jpql查询

错误消息中说明了故障原因。不能对实体的非单数属性使用批量更新,因为它们存储在不同的表中

你怎么做呢?您可以更新集合表

集合表的默认表名为
。因此,您感兴趣的表应该命名为
EmployeeImpl\u phonenumber
。默认情况下,
EmployeeImpl
(外键)的
id
列应命名为
EmployeeImpl\u id

编辑我最初发布的内容在JPQL中无效。您可能希望改用本机查询。它很简单,因此应便于携带:

然后,本机查询可能如下所示:

UPDATE EmplyeeImpl_phonenumber 
SET phonenumber = ? 
WHERE employeeimpl_id = ?

错误消息中说明了失败的原因。不能对实体的非单数属性使用批量更新,因为它们存储在不同的表中

你怎么做呢?您可以更新集合表

集合表的默认表名为
。因此,您感兴趣的表应该命名为
EmployeeImpl\u phonenumber
。默认情况下,
EmployeeImpl
(外键)的
id
列应命名为
EmployeeImpl\u id

编辑我最初发布的内容在JPQL中无效。您可能希望改用本机查询。它很简单,因此应便于携带:

然后,本机查询可能如下所示:

UPDATE EmplyeeImpl_phonenumber 
SET phonenumber = ? 
WHERE employeeimpl_id = ?

不,这在JPQL中是不可能的。正如kostja所说:消息说得很清楚,而且根据JPA规范“4.10批量更新和删除操作”一章,您可以只更新状态字段和单值对象字段

这些操作的语法如下所示:

怎么办?


可能最简洁的方法就是获取实体并添加/替换电话号码,尽管您也可以使用kostja所说的SQL查询来实现这一点。

不,这在JPQL中是不可能的。正如kostja所说:消息说得很清楚,而且根据JPA规范“4.10批量更新和删除操作”一章,您可以只更新状态字段和单值对象字段

这些操作的语法如下所示:

怎么办?


可能最简单的方法就是获取实体并添加/替换电话号码,尽管您也可以使用SQL查询,如kostja所说。

您想更新整个号码列表还是只添加一个号码?我对两者都感兴趣。我发布的查询应该用新值替换所有数字。您想更新整个数字列表还是只添加一个数字?我对两者都感兴趣。我发布的查询应该用新的值替换所有的数字。不,这是我的想法。如果它不起作用,我将删除答案。但你可能是对的,我混合了JPQL和本机SQL。我在编译该查询时出错:
未知实体类型[EmployeeImpl\u phonenumber]
我想我无法更新非实体的内容,至少不能这样。对,那么你可能必须使用本机SQL我想我不能。这必须至少在H2和PostgreSQL上起作用。不,这是我的想法。如果它不起作用,我将删除答案。但你可能是对的,我混合了JPQL和本机SQL。我在编译该查询时出错:
未知实体类型[EmployeeImpl\u phonenumber]
我想我无法更新非实体的内容,至少不能这样。对,那么你可能必须使用本机SQL我想我不能。这必须至少在H2和PostgreSQL上起作用。
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