Search code examples
bean-validationhibernate-validator

hibernate bean validation - group validation sequence


I cannot get my GroupSequence to work. I want the inner constraint's on the id property to validate first before the unique constraint's are validated.

In each interface

Entity

@UniqueNaturalId(groups = {Create.class, Update.class})
public abstract Entity
{
    @Null(groups = {PreCreate.class})
    @NotNull(groups = {PreUpdate.class, PreDelete.class })
    private Integer id;

    // other properties without groups
}

Group interfaces

@GroupSequence({PreUpdate.class, Update.class})
public interface PreUpdate {}

// PreCreate and PreDelete follow same structure

Calling groups for update operation

<property name="javax.persistence.validation.group.pre-update">
    javax.validation.groups.Default, 
    package.PreUpdate
</property>

Environment

Glassfish 3.1.2 with built-in Hibernate-Validator 4.2.


Solution

  • @GroupSequence({PreUpdate.class, Update.class})
    public interface PreUpdate {}
    

    This interface declaration will result in a cyclic Group validation.

    Creating a new group including the ordered sequence of validation groups:

    @GroupSequence({Default.class, PreUpdate.class, Update.class})
    public interface UpdateOrdered {}
    

    and replacing the groups in the hibernate configuration with the new group like following:

    <property name="javax.persistence.validation.group.pre-update">
            package.UpdateOrdered
    </property>
    

    Results now in the correct sequence of validation.

    In this example @UniqueNaturalId is validated after the inner constraints on the id property.