Bean validation @ElementCollection and @Version conflict and fails validation

Viewed 510

I am facing a very strange issue at the moment.

I have an entity that contains a property that is an element collection.

@ElementCollection(targetClass=Integer.class, fetch = FetchType.EAGER)
@CollectionTable(name="campaign_publisher", joinColumns=@JoinColumn(name="campaign_id"))
@Column(name = "publisher_id")

...

@NotEmpty(message = "campaign.publishers.missing")
public Set<Integer> getPublishers() {
    return this.publishers;
}

public Campaign setPublishers(Set<Integer> publisherId) {
    this.publishers = publisherId;
    return this;
}

This all works fine. The values are validated and saved correct.

I also want this entity to have optimistic concurrency so I applied a @Version annotation as well.

@Version
private Long etag = 0L;

...

public Long getEtag() {
    return etag;
}

public void setEtag(Long etag) {
    this.etag = etag;
}

By adding the @Version annotation the @NotEmpty validation on my set of publishers always returns invalid.

To try and diagnose this I have tried the following:

  • Creating a custom validator at the entity level so I can inspect the values in the entity. I found that the Set of values have been replaced with an empty PersistentSet which is causing the validation to always fail.

  • I created some unit tests for the entity that uses a validator that is retrieved from the validationfactory and this validator seems to work as expected.

  • I have also tried to change the ElementCollection to a many-to-many relationship and a bi-directional one-to-many but the issue persists.

Right now I am out of ideas. The only thing I have found that works correctly is disabling the hibernate validation and manually calling the validator just before I save my data.

So my questions are:

  • Has anyone encountered this issue before?
  • Any advice on what I could try next?

Thank you all for reading!

3 Answers
Related