Hibernate @ElementCollection size / null validation always fails

I am trying to create a collection of items for my latest spring boot project, but I am having trouble validating.

I want to make sure there is at least one value in the set.

I tried:

@Size(min = 1)
@NotEmpty
@NotNull

      

But every time the check fails. Even if I try to save a set with one or more items in it.

Here is the current property code 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 weird thing is that I created unit tests for this check and they work great. This was done by getting the validator from the factory validator. For example:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory()
validator                = factory.getValidator()

...

Set<ConstraintViolation<Campaign>> constraintViolations = validator.validate(campaign)

      

But when I post an object to CrudRepository to be saved when validation fails.

Also if I remove the check for this property and run the integration test, the data is saved correctly.

If you need more information, please let me know and I will provide it.

Thanks for any help!

+3


source to share





All Articles