我在Android上使用bean验证时遇到问题。我已将验证api和hibernate验证器4.0.1包含在libs和assets文件夹中。验证输入的代码如下
/* This doesn't work either
*
* ValidatorFactory factory = Validation
* .byProvider(HibernateValidator.class).configure()
* .buildValidatorFactory();
*/
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Class<Department>>> violations = validator
.validate(Department.class);
for (ConstraintViolation<Class<Department>> v : violations) {
// do something useful
}
我收到一个ValidationException,它声明没有找到默认提供程序。评论部分也不起作用。它还抛出一个ValidationException,声明它无法找到提供者org.hibernate.validator.HibernateValidator。
02-04 07:44:22.388: E/AndroidRuntime(946): Caused by: javax.validation.ValidationException: Unable to find provider: class org.hibernate.validator.HibernateValidator
02-04 07:44:22.388: E/AndroidRuntime(946): at javax.validation.Validation$ProviderSpecificBootstrapImpl.configure(Validation.java:223)
02-04 07:44:22.388: E/AndroidRuntime(946): at com.example.testapp.MainActivity.validate(MainActivity.java:51)
02-04 07:44:22.388: E/AndroidRuntime(946): at com.example.testapp.MainActivity.anlegen(MainActivity.java:46)
02-04 07:44:22.388: E/AndroidRuntime(946): ... 14 more
配置时我错过了什么吗?或者hibernate不适用于android?
更新
我尝试实现ValidatorProviderResolver为@Gunnar建议。代码更改为:
HibernateValidatorConfiguration config = Validation
.byProvider(HibernateValidator.class)
.providerResolver(new MyAndroidValidationProviderResolver())
.configure();
ValidatorFactory factory = config.buildValidatorFactory();
Validator v = factory.getValidator();
现在我得到的例外情况如下:
02-05 20:21:36.642: E/AndroidRuntime(792): Caused by: java.lang.IllegalArgumentException: Invalid logger interface org.hibernate.validator.internal.util.logging.Log (implementation not found)
02-05 20:21:36.642: E/AndroidRuntime(792): at org.jboss.logging.Logger.getMessageLogger(Logger.java:2250)
02-05 20:21:36.642: E/AndroidRuntime(792): at org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
02-05 20:21:36.642: E/AndroidRuntime(792): at org.hibernate.validator.internal.util.logging.LoggerFactory.make(LoggerFactory.java:29)
02-05 20:21:36.642: E/AndroidRuntime(792): at org.hibernate.validator.internal.util.Version.<clinit>(Version.java:27)
02-05 20:21:36.642: E/AndroidRuntime(792): ... 21 more
我还切换到Hibernate 5.0.3 final,包括所有库(必需和可选)。
答案 0 :(得分:1)
org.hibernate.validator.internal.util.logging.Log接口使用Android不支持的2个类: javax.xml.bind.JAXBException和javax.xml.stream.XMLStreamException。这就是没有加载课程的原因。