Unique constraint with JPA and Bean Validation

前端 未结 4 1943
南方客
南方客 2020-11-30 02:25

I\'d like to have a @Unique constraint with Bean Validation, but that is not provided by the standard. If I would use JPA\'s @UniqueConstraint I wo

4条回答
  •  醉酒成梦
    2020-11-30 02:48

    You can make a validator read the JPA annotations and apply it. Here is somewhat of an example using spring validators that can be used as an idea to expand on.

    JPA JSR303 Spring Form Validation

    You can also inject (@Inject or Spring's @Autowired) your session bean in a custom validator and the container should know how to wire it up. I only know this as a Spring example:

    import javax.validation.ConstraintValidator;
    
    public class MyConstraintValidator implements ConstraintValidator {
    
    @Autowired //@Inject
    private Foo aDependency;
    
    ...
    }
    

提交回复
热议问题