Unique Validator with JSF2 and JPA2 but without Bean Validation
Such a classic! One of your form’s fields must be unique in the database.
As we use JPA2 and bean validation, our fist thought was let’s use bean validation.
But soon, we realized this is not as easy as it should be:
- Bean Validation, does not provide any unique validator for JPA (ok I am a bit exigent)
- from the validator implementation you don’t have access to the entity name
- you cannot inject beans within Bean Validation validator implementation. For instance, you cannot inject the entityManager.
Hopefully, your devoted team has found a solution and is sharing it here with you (and in the projects generated by springfuse)
Instead of annotating an entity property, we leverage the
<f:validateBean> tag directly in the view.
Here is an usage example:
When an already existing username is entered, the validator fails and a nice error message is displayed.
Here is the
jpaUniqueValidator component source code:
It invokes the
jpaUniqueSupport.isUnique(entity, property, value) method which is listed below:
The SpringFuse/Jaxio team.
comments powered by