Search code examples
javaspringjpaspring-data-jpaspring-data

When updating an ElementCollection does it trigger the @PostUpdate of the containing object?


I have an @ElementCollection Map<User, Long> permissions in a class.

@ElementCollection
@CollectionTable(name = "als_permission", joinColumns = @JoinColumn(name = "File"))
@MapKeyJoinColumn(name = "User")
@Column(name = "Permission")
@JsonIgnore
private Map<User, Integer> permissions = new HashMap<>();

I made some changes on that collection only, and invoke repo.save(entity). I see the record does get updated, but my @PostUpdate handler which is defined in @EntityListeners does not appear to be called.

Is there something I have to put to indicate it? I am thinking I may need to have some sort of cascade somewhere.


Solution

  • Short Answer: No.

    I had the same issue came to the conclusion that this is not possible at the moment. Mainly because intended or not implemented yet, see: https://github.com/eclipse-ee4j/jpa-api/issues/167

    For the testing I created a repository to find any event I could consumer for the given purpose: https://github.com/HannesRakete/so-jpa-element-collection-events

    Workaround 1

    Add a @Version for optimistic locking on the parent entity.

    Workaround 2

    Migrate to another association-type, see https://thorben-janssen.com/hibernate-tips-query-elementcollection/