JPQL查询批量更新设置在ElementCollection字段上 [英] JPQL Query Bulk UPDATE SET on an ElementCollection field

查看:173
本文介绍了JPQL查询批量更新设置在ElementCollection字段上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要更新的JPA实体:

I have the following JPA Entity I want to update:

@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;
}

我以为我像这样使用namedQuery

@NamedQuery(name = "updateEmployee",
    query = "Update EmployeeImpl e SET e.phonenumber :number WHERE e.id = :id")

但这不起作用:Exception Description: Error compiling the query [updateEmployee: Update EmployeeImpl e SET e.phonenumber = :number WHERE e.id = :id], line 1, column 28: invalid access of attribute [phonenumber] in SET clause target [e], only state fields and single valued association fields may be updated in a SET clause.

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

Question is, how do I update an @ElementCollection? If it's possible i'd like to do it with a jpql query.

推荐答案

否,这在JPQL中是不可能的.就像kostja所说:消息说得很清楚,并且根据JPA规范"4.10批量更新和删除操作"一章,您只能更新状态字段和单个值对象字段.

No, that is not possible in JPQL. As kostja says: the message says it clear and also according to the JPA specification, Chapter "4.10 Bulk Update and Delete Operations" you may update only state fields and single values object fields.

这些操作的语法如下:

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

做什么?

可能最干净的方法是简单地获取实体并添加/替换电话号码,尽管您始终可以使用

Probably the most clean way to do that is simply to fetch the entities and to add/replace the phone number/s, although you can always do that also with Native Queries, i.e SQL queries as kostja says.

这篇关于JPQL查询批量更新设置在ElementCollection字段上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆