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
java spring hibernate


source to share


No one has answered this question yet

Check out similar questions:

323
How to solve "failed to lazily initialize role collection" Hibernate exception
13
ElementCollection createAlias โ€‹โ€‹in hibernate API
3
Hibernate does not create ElementCollection table
3
Bean validation @ElementCollection and @Version conflict and invalid validation
2
Hibernate @MappedSuperClass with ElementCollection
0
Hibernate factory factory is always null
0
hibernate update always null
0
Hibernate @ElementCollection size constraints
0
Unable to insert NULL value into "trackerId" column, "History" table; the column does not allow zeros. INSERT doesn't work
0
Save parent and child entities with notnull foreignkey towards parent (Spring JPA / Hibernate)



All Articles
Loading...
X
Show
Funny
Dev
Pics