一.校验组序列

默认情况下,约束的验证是没有一定的顺序的,不管他们是属于哪个认证组的.但是在有些环境中,我们控制这些约束验证的顺序还是很有用的.

就拿我们上一个例子来说,我们可以这样:首先在我们检查车的性能之前关于车的默认的约束应该是验证通过的,然后,在我们行驶之前,我们应该检查驾驶员的相关约束条件.为了实现这样的认证顺序,我们要定义一个新的接口,在这个接口上面加上一个@GroupSequence 注释,用来定义我们验证组执行的顺序

注意:验证组中如果有一个约束失败了,那么后面的约束将都会认为是失败的.

如下:

  1. @GroupSequence({Default.class, CarChecks.class, DriverChecks.class})
  2. public interface OrderedChecks {
  3. }

警告
一个校验组序列中包含的校验组和这个校验组序列不能造成直接或者间接的循环
引用. 包括校验组继承. 如果造成了循环引用的话, 会导
致 GroupDefinitionException 异常.

定义好了之后,就来使用:

  1. @Test
  2. public void testOrderedChecks() {
  3. Car car = new Car( "Morris", "DD-AB-123", 2 );
  4. car.setPassedVehicleInspection( true );
  5. Driver john = new Driver( "John Doe" );
  6. john.setAge( 18 );
  7. john.passedDrivingTest( true );
  8. car.setDriver( john );
  9. assertEquals( 0, validator.validate( car, OrderedChecks.class ).size() );
  10. }

二.对一个类重定义其默认校验组

这个@GroupSequence的注释还有第二种作用,它可以用来让你重新定义一个类的默认组的方法.为了重新定义类.我们只需要在这个类上加一个@GroupSequence注解

这个注解中定义的组的组序列将会替代这个类的默认的.

例:

第一步.先来一个出租车的认证组

  1. package test01;
  2.  
  3. /**
  4. * @author Administrator
  5. *出租车认证组
  6. */
  7. public interface RentalChecks {
  8.  
  9. }

2.再来一个出租车类

  1. package test01;
  2.  
  3. import javax.validation.GroupSequence;
  4. import javax.validation.constraints.AssertFalse;
  5.  
  6. @GroupSequence({ RentalChecks.class, CarChecks.class, RentalCar.class })
  7. public class RentalCar extends Car {
  8. /**
  9. * 是否已经出租
  10. */
  11. @AssertFalse(message = "The car is currently rented out", groups = RentalChecks.class)
  12. private boolean rented;
  13.  
  14. public RentalCar(String manufacturer, String licencePlate, int seatCount) {
  15. super(manufacturer, licencePlate, seatCount);
  16. }
  17.  
  18. public boolean isRented() {
  19. return rented;
  20. }
  21.  
  22. public void setRented(boolean rented) {
  23. this.rented = rented;
  24. }
  25. }

注意:由于在认证组中不能有循环依赖且当给一个类重新定义组序列时认证组序列中不能加入默认(Default)组,所以我们需要将这个类本身添加进去,如上例!

3.最后进行测试

@Test
public void carIsRented() {
RentalCar rentalCar = new RentalCar("Morris", "DD-AB-123", 2);
rentalCar.setPassedVehicleInspection(true);
rentalCar.setRented(true);
Set<ConstraintViolation<RentalCar>> constraintViolations = validator.validate(rentalCar);
assertEquals(1, constraintViolations.size());
assertEquals("Wrong message", "The car is currently rented out",
constraintViolations.iterator().next().getMessage());
rentalCar.setRented(false);
constraintViolations = validator.validate(rentalCar);
assertEquals(0, constraintViolations.size());
}

注意:

当在一个类上加@GroupSequence时,它是不会传递到相关的对象中,这意味着如果你在上面的例子中添加了DriverChecks 是没有效果的.只有当认证一个实例的时候,才会有效果

三.@GroupSequenceProvider

关于前面的@javax.validation.GroupSequence 注解是一个标准的Bean认证注解,正如前面所见的它能够让你静态的重新定义一个类的默认认证组的组顺序,Hibernate Validator同时也提供了一个可以自己操作的,非标准的注解org.hibernate.validator.group.GroupSequenceProvider,它能够让你动态的重新定义一个组的顺序.

1.先创建一个GroupSequenceProvider用来动态选择认证组

  1. public class RentalCarGroupSequenceProvider implements DefaultGroupSequenceProvider<RentalCar> {
  2. public List<Class<?>> getValidationGroups(RentalCar car) {
  3. List<Class<?>> defaultGroupSequence = new ArrayList<Class<?>>();
  4. defaultGroupSequence.add( RentalCar.class );
  5. if ( car != null && !car.isRented() ) {
  6. defaultGroupSequence.add( CarChecks.class );
  7. }
  8. return defaultGroupSequence;
  9. }
  10. }

2.使用这个GroupSequenceProvider

  1. @GroupSequenceProvider(RentalCarGroupSequenceProvider.class)
  2. public class RentalCar extends Car {
  3. @AssertFalse(message = "The car is currently rented out", groups = RentalChecks.class)
  4. private boolean rented;
  5. public RentalCar(String manufacturer, String licencePlate, int seatCount) {
  6. super( manufacturer, licencePlate, seatCount );
  7. }
  8. public boolean isRented() {
  9. return rented;
  10. }
  11. public void setRented(boolean rented) {
  12. this.rented = rented;
  13. }

hibernate_validator_07的更多相关文章

随机推荐

  1. Qt之模型/视图(自定义按钮)(使用QStyleOption的子类进行drawControl,和我用的方法完全不一样)

    http://blog.csdn.net/liang19890820/article/details/50974059

  2. Struts2 cookie的存取

    /** * Cookieの追加 * @return * @throws Exception */ private void addCookie(String name,String value){ C ...

  3. Gap Locks 区间锁

    Gap Locks 区间锁 1. 区间锁不能用于语句锁定记录使用一个唯一索引来搜索一个唯一的记录 2.READ COMMITTED 没有区间锁 区间锁是一个锁在一个在index记录间的区间,或者一个l ...

  4. 账户管理groupadd groupmod groupdel usermod usermod userdel

    http://www.cnblogs.com/ggjucheng/archive/2012/08/21/2648380.html http://blog.csdn.net/qq1603013767/a ...

  5. 【转】eclipse -- the project was not built due to a resource exists with a different case...

    原文网址:http://blog.csdn.net/mylinx/article/details/44280563 进行编码时,工程前面莫名有个红X,正当百思不得其解时,发现在[problems]下有 ...

  6. 【模拟】NEERC15 A Adjustment Office (2015-2016 ACM-ICPC)(Codeforces GYM 100851)

    题目链接: http://codeforces.com/gym/100851 题目大意: 一个N*N的矩阵A,Ai,j=i+j,Q次操作,每次分两种,R r取出第r行还未被取的所有数,并输出和.C c ...

  7. Construct Binary Tree from Inorder and Postorder Traversal——LeetCode

    Given inorder and postorder traversal of a tree, construct the binary tree. 题目大意:给定一个二叉树的中序和后续序列,构建出 ...

  8. 高效算法——C 分饼

    My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N ...

  9. Nodejs in Visual Studio Code 09.企业网与CNPM

    1.开始 CNPM : https://npm.taobao.org/ 2.企业网HTTP代理上网 平时办公在一个大企业网(10.*.*.*)中,使用HTTP代理上网,发现npm命令无法执行. 解决方 ...

  10. TXT四则运算计算器

    基本思想:使用getline函数从TXT文件中依次读出中缀表达式,将其转为后缀表达式后计算结果,并与用户结果比对. 整数.分数.小数的处理:将小数和整数都视为默认分母为1的分数.建立分数类,在中缀转换 ...