Hibernate validations on save (insert) only
Asked Answered
F

1

6

We encountered a problem with legacy code. There is a validation set for a "username" field, validating its length and making sure it contains at least one letter:

@Column(name = "username")
@Size(min = 4, max = 40)
@Pattern(regexp = "^.*[a-zA-Z]+.*$")
private String username;

The problem we have is that some existing legacy data do not fit these validations, and I'm trying to find a way to make these validations to be ignored for legacy data (old users), while still be applied to newly created users.

I was thinking about moving the validations to setUsername(...) method (so value will be validated on an actual change only), but this caused an exception:

javax.validation.ValidationException: Annotated methods must follow the JavaBeans naming convention. setUsername() does not.

I also made sure the entity is set to dynamicUpdate=true, but this doesn't help since hibernate is validating all properties, even if no change occurred.

How can I prevent these validations to be performed on existing entities during update?
I do not want the fix to impact other properties validations on the same entity and I can't change hibernate configuration.

Fortyfive answered 14/5, 2017 at 12:47 Comment(0)
F
10

After two days of research I found out how to make this work.
Apparently, specifying validations that would be validated on INSERT only is not that difficult. The only changes required are to set these validations to a specific validation group and to validate this group during INSERT/pre-persist events.

First of all I created an interface called platform.persistence.InsertOnlyValidations to be used as a group which will be validated during pre-persist only.

Than, I added the group to the username field validations:

@Column(name = "username")
@Size(min = 4, max = 40, groups = {InsertOnlyValidations.class})
@Pattern(regexp = "^.*[a-zA-Z]+.*$", groups = {InsertOnlyValidations.class})
private String username;

This instructs hibernate not to use these validations as part of the default group. Now, I needed to instruct hibernate to validate these validation rules during insert only.
The way to do that is very simple, I needed to pass the property javax.persistence.validation.group.pre-persist, while indicating which groups will be validated during a pre-persist event:

javax.persistence.validation.group.pre-persist=javax.validation.groups.Default,platform.persistence.InsertOnlyValidations

This instructs hibernate that during a pre-persist event all default validations will be validated (javax.validation.groups.Default) in addition to all the validations included in the InsertOnlyValidations group.

Fortyfive answered 15/5, 2017 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.