1

I'm new to hibernate validator

I would like to have one annotation and many implementations. Problem is, that annotation should be placed in domain (api) module and implementations in corresponding implementation modules (xx-command, xx-query). So the parameter validatedBy of @Constraint annotation has to be empty.
Can I somehow dynamically tell to hibernate to use all implementations of specified annotation which can pass type which I need. Also I would like to have implementations in more than one submodule.

@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {})
public @interface CanNotExists {

    String message() default "com.foo.api.domain.validation.annotation.CanNotExists.message";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

this is my project structure (in dependency order accessor-command depends on accessor-query, accessor-query dependes on infrastrucutre ...)
-api
-infrastructure
-accessor-query
-accessor-command

I found this question and wanted to use xml configuration (see the edit section in answer), but it's not helpful because I would like to use implementation in many modules >>> as mentioned here

A given entity can only be configured once across all configuration files. The same applies for constraint definitions for a given constraint annotation. It can only occur in one mapping file. If these rules are violated a ValidationException is thrown.

So I can't split constraints to multiple xml configurations.

Basic need of this validator is to look into repository and validate if specified object already exists. I'm also using spring in my project so if somebody have better approach how to do this (even with spring or another framework), it will be welcome.

Thanks

Community
  • 1
  • 1
bilak
  • 4,526
  • 3
  • 35
  • 75

1 Answers1

0

Your question is tagged with 'spring', so may be you can use springs validation that under the hood uses hibernate :

[spring validation][1]

Let your validator implement the interface spring Validator, create a bean of it, thats it.