Search code examples
dictionaryjpaentityjpql

JPQL query updating CollectionTable


I have the following entity:

@Entity(name = "Directory")
@Table(name = "Directory")
public Directory {

@Id
@Column(name = "id")
protected String id;

@ElementCollection
@MapKeyColumn(name = "file")
@CollectionTable(name = "Directory_Files", joinColumns = @JoinColumn(name = "id"))
@Column(name = "hash")
protected Map<File, String> fileToHash;

...

}

The hash of a file of a directory should be updated using a JPQL query.

I have tried the following query but it's not working:

UPDATE Directory t SET t.Directory_Files.hash = :hash WHERE t.Directory_Files.id = :id AND t.Directory_Files.file = :file

Does anyone found the mistake in this query?

Any help is appreciated.


Solution

  • "Directory_Files" is a database table and doesn't exist in your object model, and so cannot be used in JPQ - You would need to access the fileToHash Map within Directory

    Unfortunately JPQL bulk update statements define update Item to be: update_item ::= [identification_variable.]{single_valued_embeddable_object_field.}* {state_field | single_valued_object_field} = new_value

    fileToHash is a collection and so cannot be used in an Update statement.

    You will need to use an SQL query and pass it to EntityManager.createNativeQuery(sqlString)