HV000054: [Type] must be part of the redefined default group sequence


#1

I’m trying to use a GroupSequenceProvider, based on a simplified version of what are in the docs, but am getting an error. Wondering if anyone knows what’s wrong.

import javax.validation.constraints.NotEmpty;
import org.hibernate.validator.group.GroupSequenceProvider;

@GroupSequenceProvider(CarGroupSequenceProvider.class)
public class Car {

  @NotEmpty
  private String make;

  @NotEmpty(groups = RegistrationGroup.class)
  private String vin;

  public Car(String make) { this.make = make; }
  public String getMake() { return make; }
  public void setMake(String make) { this.make = make; }
  public String getVin() { return vin; }
  public void setVin(String vin) { this.vin = vin; }
}
public interface RegistrationGroup {}
import java.util.ArrayList;
import java.util.List;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

public class CarGroupSequenceProvider implements DefaultGroupSequenceProvider<Car> {

  @Override
  public List<Class<?>> getValidationGroups(Car car) {
    List<Class<?>> defaultGroupSequence = new ArrayList<>();
    defaultGroupSequence.add(RegistrationGroup.class);
    return defaultGroupSequence;
  }

}
    ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
        .configure()
        .constraintValidatorPayload( "US" )
        .buildValidatorFactory();

    Validator validator = validatorFactory.getValidator();

    Car car = new Car("Tesla");

    Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );

Results in:

javax.validation.GroupDefinitionException: HV000054: Car must be part of the redefined default group sequence.
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.getValidDefaultGroupSequence(BeanMetaDataImpl.java:514)
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.getDefaultGroupSequence(BeanMetaDataImpl.java:339)
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.<init>(BeanMetaDataImpl.java:237)
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl$BeanMetaDataBuilder.build(BeanMetaDataImpl.java:651)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:192)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManager.lambda$getBeanMetaData$0(BeanMetaDataManager.java:160)
	at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanMetaData(BeanMetaDataManager.java:159)
	at org.hibernate.validator.internal.engine.ValidationContext$ValidationContextBuilder.forValidate(ValidationContext.java:566)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:155)

The error sounds like I need to include javax.validation.groups.Default in the list, so I’ve also tried:

import java.util.ArrayList;
import java.util.List;
import javax.validation.groups.Default;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;


public class CarGroupSequenceProvider implements DefaultGroupSequenceProvider<Car> {

  @Override
  public List<Class<?>> getValidationGroups(Car car) {
    List<Class<?>> defaultGroupSequence = new ArrayList<>();
    defaultGroupSequence.add(Default.class);
    defaultGroupSequence.add(RegistrationGroup.class);
    return defaultGroupSequence;
  }

}

But it generates the following error:

javax.validation.GroupDefinitionException: HV000053: 'Default.class' cannot appear in default group sequence list.
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.getValidDefaultGroupSequence(BeanMetaDataImpl.java:506)
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.getDefaultGroupSequence(BeanMetaDataImpl.java:339)
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.<init>(BeanMetaDataImpl.java:237)
	at org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl$BeanMetaDataBuilder.build(BeanMetaDataImpl.java:651)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:192)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManager.lambda$getBeanMetaData$0(BeanMetaDataManager.java:160)
	at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
	at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanMetaData(BeanMetaDataManager.java:159)
	at org.hibernate.validator.internal.engine.ValidationContext$ValidationContextBuilder.forValidate(ValidationContext.java:566)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:155)

#2

When redefining the default group sequence of a bean, the default group is represented by the bean class.

So what’s needed is that you should add Car.class to your group sequence as mentioned in the error message.


#3

@gsmet That worked. Many thanks for letting me know.

It might be worthwhile to mention in the docs, that the class itself is used to represent the default group and that it’s required to be in the List returned. Groups are usually defined individually and I wouldn’t have guessed that they would have served a dual purpose for these.

For the purpose of internet history, here’s the working DefaultGroupSequenceProvider:

import java.util.ArrayList;
import java.util.List;
import javax.validation.groups.Default;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

public class CarGroupSequenceProvider implements DefaultGroupSequenceProvider<Car> {

  @Override
  public List<Class<?>> getValidationGroups(Car car) {
    List<Class<?>> defaultGroupSequence = new ArrayList<>();
    defaultGroupSequence.add(Car.class);
    defaultGroupSequence.add(RegistrationGroup.class);
    return defaultGroupSequence;
  }

}