Charlee Chitsuk
Charlee Chitsuk

Reputation: 9059

JSR 303 Bean Validation: The Constraint composition with groups for each composited constraint

I'm trying to use the Constraint composition and would like to define the group for each composited constraint as the following example: -

The Composite Constraint

@Target({
    ElementType.FIELD,
    ElementType.METHOD,
    ElementType.PARAMETER,
    ElementType.TYPE,
    ElementType.ANNOTATION_TYPE
})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DummyValid1(group = Group1.class)
@DummyValid2(group = Group2.class)
@Constraint(validatedBy = {})
public @interface DummyCompositeValid {

    String message() default "The bean is invalid.";

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

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

The data bean

@DummyCompositeValid 
public class DummyBean {
    //Fields declaration, Setter and Getter
}

The business validation method

this.validator.validate(dummyBean, Group1.class, Group2.class);

The result is the dummyBean has not been validated. I'm confused since the group is worked well when it is defined directly without the composite constraint as the following: -

The data bean

@DummyValid1(group = Group1.class)
@DummyValid2(group = Group2.class)
public class DummyBean {
    //Fields declaration, Setter and Getter
}

Could you please help to advise and explain further? Thank you very much for your help in advance. I'm looking forward to hearing from you soon.

Regards,

Charlee Ch

Upvotes: 3

Views: 2472

Answers (1)

Charlee Chitsuk
Charlee Chitsuk

Reputation: 9059

I've asked this question to the Hibernate validator forum and would like to inform us the result as the following: -

What I am trying to do is not possible. The composing constraints are inheriting the groups from the main annotation. This is part of the spec:

Groups from the main constraint annotation are inherited by the composing annotations. Any groups definition on a composing annotation is ignored. Likewise, payload from the main constraint annotation is inherited by the composing annotations. Any payload definition on a composing annotation is ignored.

Upvotes: 3

Related Questions