hibernate验证器嵌套类与xml映射

时间:2012-11-19 16:41:18

标签: xml hibernate java-ee

非常极端的情况,谷歌搜索似乎没有帮助。

我必须使用xml验证(生成的代码),场景类似于:

 // This file was generated by the JavaTM Architecture for XML
 Binding(JAXB) Reference Implementation, v2.2.4 

 ...

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "xxx", propOrder
 = { ... }) @XmlRootElement(name = "xxx") public class Class01DTO
     implements Serializable { ...
     @XmlElement(required = true)
     protected BetslipDTO.SlipPlacement slipPlacement;

     ...

     @XmlAccessorType(XmlAccessType.FIELD)
     @XmlType(name = "", propOrder = { ...
     })
     public static class Class02
         implements Serializable
     {

         private final static long serialVersionUID = ...;

         protected String stringToValidate;


     }

 }

我对如何验证" stringToValidate"没有信心。我有一个constraints-mapping.xml,类似于:

<bean class="com.etc.Class01DTO.Class02">
    <field name="stringToValidate">
        <constraint annotation="javax.validation.constraints.NotNull">
        </constraint>
    </field>
</bean>

这只会产生错误。已经尝试了不同的组合,但没有结果。有没有人遇到过同样的案子? 我假设很明显代码是生成的,所以不能使用基于注释的选项...

执行构建时,错误类似于以下内容 ...

  shouldValidateSpringContext(com.etc.TestBetSpringContext): Error creating bean with name 'validatorFactoryBean' defined in class path resource [spring/bet-service.xml]: Invocation of init method failed; nested exception is javax.validation.ValidationException: HV000065: Unable to load class: com.etc.Class01DTO.Class02.

1 个答案:

答案 0 :(得分:2)

如果你想指定一个嵌套的类名,你应该使用com.etc.Class01DTO$Class02,在编译这些类之后,看看目标目录,你会发现所有嵌套的类都被编译成单独的文件{ {1}}在他们的名字中。