Let\'s assume that I have a form where I might submit username(@NaturalId)
and password
for a new user.
I would like to add the user with a
JSR-303 does not support what you want (something like a @Unique
constraint). You have to write you own validator. How can this be done is explained here: https://community.jboss.org/wiki/AccessingtheHibernateSessionwithinaConstraintValidator
But before doing that make sure you read this answer: https://stackoverflow.com/a/3499111/1981720
And this sentence from the previous article:
The reason @Unique is not part of the built-in constraints is the fact that accessing the Session/EntityManager during a valiation is opening yourself up for potenital phantom reads.