I'm trying to set up an element collection for my latest spring boot project but I am having issues with the validation.
I want to make sure there is at least one value in the set.
I have tried:
@Size(min = 1)
@NotEmpty
@NotNull
But every time the validation fails. Even if I try and save a set with one or more items in it.
Here is the current code of the property in my entity.
@ElementCollection(targetClass=Integer.class, fetch = FetchType.EAGER)
@CollectionTable(name="campaign_publisher", joinColumns=@JoinColumn(name="campaign_id"))
@Column(name="publisher_id", nullable = false)
private Set<Integer> publishers = new HashSet<>();
The strange thing is that I have created unit tests for this validation and they work fine. This was done by getting a validator from the validator factory. For example:
ValidatorFactory factory = Validation.buildDefaultValidatorFactory()
validator = factory.getValidator()
...
Set<ConstraintViolation<Campaign>> constraintViolations = validator.validate(campaign)
But when I send the entity to CrudRepository to be saved thats when the validation fails.
Also if I remove the validation on this property and run an integration test the data is saved correctly.
If you need anymore info let me know and I will provide it.
Thanks for any help!