-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: about validation -- Class-level constraints @PassengerCount
PostPosted: Tue Jul 03, 2012 5:41 am 
Newbie

Joined: Tue Jul 03, 2012 5:34 am
Posts: 1
I have read the article below
http://docs.jboss.org/hibernate/validat ... le/#d0e326

"
2.1.3. Class-level constraints

Last but not least, a constraint can also be placed on class level. When a constraint annotation is placed on this level the class instance itself passed to the ConstraintValidator. Class level constraints are useful if it is necessary to inspect more than a single property of the class to validate it or if a correlation between different state variables has to be evaluated. In Example 2.3, “Class level constraint” we add the property passengers to the class Car. We also add the constraint PassengerCount on the class level. We will later see how we can actually create this custom constraint (see Chapter 3, Creating custom constraints). For now we it is enough to know that PassengerCount will ensure that there cannot be more passengers in a car than there are seats. "


the question is how to implment PassengerCount? . Thanks!


Top
 Profile  
 
 Post subject: Re: about validation -- Class-level constraints @PassengerCount
PostPosted: Wed Jul 04, 2012 4:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
It is just like any custom constraint. I admit the concrete implementation is not in the docs, but it is really not hard:

Code:
@Target( { TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = PassengerCountValidator.class)
@Documented
public @interface PassengerCount {
    String message() default "{foo}";

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

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


and

Code:
public class PassengerCountValidator implements ConstraintValidator<PassengerCount, Car> {

    public void initialize(PassengerCount constraintAnnotation) {
    }

    public boolean isValid(Car car, ConstraintValidatorContext constraintContext) {
        if (car == null)
            return true;

       return car.getPassengers().size() < 5   
    }

}


That said, I also created a Jira issue for clearing up the documentation - https://hibernate.onjira.com/browse/HV-604

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.