Search code examples
jpaeclipselinkjpql

Updating a one entity with another entity using JPQL update method


I tried to replace an entity with one to many relationship with another entity in JPQL, but do not know how to phrase the JPQL correctly.

update PersonInstitution pi set pi.designation = (select d from Designation d where d.id = 50) where pi.designation.id = 34]

I wanted to replace one designation(with id 34) with another designation (with id 50) of all the PersonInstitution entities. But it gives errors.

I could load all objects and edit them and persist them back, but I thought it as a less efficient way.

I use JPA with EclipseLink 2.0


Solution

  • According to the specification, it might not work with JPQL.

    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 
    

    You have to do it manually, else can try using a native query instead.