3

I have an entity object with some fields (first name, last name, city, etc.). In one case I need to apply to this object one set of restrictions(not null, length restriction, unique and so on), in other case other set. At the moment I am using my own validator, which has different validation methods for different use cases.

Can I perform validation in another different way (standard solution)?

I use Spring with Hibernate. JSR303 standard which is implemented in Hibernate can't resolve my problem, because annotations allow to define only one set of restrictions which is static. I can't redefine it at runtime.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mvb13
  • 1,514
  • 3
  • 18
  • 33
  • Have you looked into this http://www.hibernate.org/subprojects/validator.html ? Or this http://stackoverflow.com/questions/397852/java-validation-frameworks ? – Rubén Apr 16 '13 at 13:23
  • @Khanser: You comment is not really useful you simply point to a list of JSR303 validators and Hibernate Validator which the author is already aware of. Unless you know if it's doable or not this does not help. – philnate Apr 16 '13 at 13:31

2 Answers2

5

You have the possibility with Hibernate Validator to construct constraints programmatically through the ConstraintMapping: http://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/cfg/ConstraintMapping.html. Documentation can be found http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/programmaticapi.html

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Car.class )
    .property( "manufacturer", FIELD )
        .constraint( NotNullDef.class )
    .property( "licensePlate", FIELD )
        .constraint( NotNullDef.class )
        .constraint( SizeDef.class )
            .min( 2 )
            .max( 14 )
    .property( "seatCount", FIELD )
        .constraint( MinDef.class )
            .value ( 2 )
.type( RentalCar.class )
    .property( "rentalStation", METHOD)
        .constraint( NotNullDef.class );
HibernateValidatorConfiguration config = Validation.byProvider( HibernateValidator.class ).configure();
config.addMapping( mapping );
ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();
philnate
  • 1,506
  • 2
  • 21
  • 39
1

You can redefine Hibernate validators at deployment-time, using a validation.xml file at the root of the classpath. If any one deployment needs but one validation scheme, that will work; you can build each deployment with its own xml file.

On the other hand, if the needed validations depend on the application's state, then you need validators that are state-dependant. Validation groups won't help you. Can you use different, but related entity objects:

abstract class BaseEntity {
    private String name;
    public String getName() { return name;}
}

public class Entity1 extends BaseEntity {
    @Override
    @NotNull
    @Length(min=2, max=20)
    public String getName() { return super.getName();)
}

public class Entity2 extends BaseEntity {
    @Override
    @Length(min=2, max=10)
    public String getName() { return super.getName();)
}

I've done this where two related classes had a list. In one, the list needed at least two elements. In the other, the list could be empty or it would need at least two elements.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
  • I can't use this approach because my Hibernate entity maps on the database table. On start-up I will receive table duplicate, but this is not that I need. – mvb13 Apr 17 '13 at 07:15