Unable to build the default ValidatorFactory(rewrite)

ssh整合过程中,由于版本不相近遇到了两个问题,特此记录下:

1.java.lang.NoSuchFieldError: INSTANCEat org.hibernate.type.BasicTypeRegistry.(BasicTypeRegistry.java:94)at org.hibernate.type.TypeResolver.(TypeResolver.java:59)at org.hibernate.cfg.Configuration.(Configuration.java:249)at org.hibernate.cfg.Configuration.(Configuration.java:300)

经过查找发现是hibernate-annotations.jar和hibernate-commons-annotations.jar的问题,hibernate.jar已经将其整合

2.去掉两个包之后出现

Error creating bean with name 'userManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bolo.examples.dao.base.UserDao com.bolo.examples.service.base.UserManager.userDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [E:\MyEclipse9.0\workspace\ssh2\WebRoot\WEB-INF\classes\com\bolo\examples\dao\base\UserDao.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to get the default Bean Validation factory

也是一番查找之后发现在






true
true
org.hibernate.dialect.MySQLDialect
none



加入红色部分就没有问题了

真是不同的版本差异比较大啊

其实这个问题是我们自己造成的!为什么这么说?因为我们在配置Spring和Hibernate进行结合的时候版本出现了问题。

原因参考下文,这是hibernate官方文档的一段话!

意思就是在hibernate.cfg.xml或者是
persistence.xml文件下面需要配置
javax.persistence.validation.mode属性!

特别的!在Hibernate中默认的

none
是auto而不是none!

------------------------------------------------------------------------------------------------------------------------------------------

javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation**包,但是找不到,所以beanvalitionFactory错误。
由于javax.persistence.validation.mode的属性值默认是auto,所以会出错。

在hibernate.cfg.xml里将javax.persistence.validation.mode设置为none,就可以避免出错了。


none

------------------------------------------------------------------------------------------------------------------------------------------

所以,Hibernate 3.6以上版本在用junit测试时会提示错误:

Unable to get the default Bean Validation factory

在hibernate.cfg.xml里增加一属性解决:

none

具体原因

  1. 23.1.1. Adding Bean Validation
  2. To enable Hibernate's Bean Validation integration, simply add a Bean Validation provider (preferably Hibernate Validation 4) on your classpath.
  3. 23.1.2. Configuration
  4. By default, no configuration is necessary.
  5. The Default group is validated on entity insert and update and the database model is updated accordingly based on the Default group as well.
  6. You can customize the Bean Validation integration by setting the validation mode. Use the javax.persistence.validation.mode property and set it up for example in your persistence.xml file or your hibernate.cfg.xml file. Several options are possible:
  7. auto (default): enable integration between Bean Validation and Hibernate (callback and ddl generation) only if Bean Validation is present in the classpath.
  8. none: disable all integration between Bean Validation and Hibernate
  9. callback: only validate entities when they are either inserted, updated or deleted. An exception is raised if no Bean Validation provider is present in the classpath.
  10. ddl: only apply constraints to the database schema when generated by Hibernate. An exception is raised if no Bean Validation provider is present in the classpath. This value is not defined by the Java Persistence spec and is specific to Hibernate.
  11. 注意
  12. You can use both callback and ddl together by setting the property to callback, dll
  13. <persistence ...>
  14. <persistence-unit ...>
  15. ...
  16. <properties>
  17. <propertyname="javax.persistence.validation.mode"
  18. value="callback, ddl"/>
  19. properties>
  20. persistence-unit>
  21. persistence>
  22. This is equivalent to auto except that if no Bean Validation provider is present, an exception is raised.
  23. If you want to validate different groups during insertion, update and deletion, use:
  24. javax.persistence.validation.group.pre-persist: groups validated when an entity is about to be persisted (default to Default)
  25. javax.persistence.validation.group.pre-update: groups validated when an entity is about to be updated (default to Default)
  26. javax.persistence.validation.group.pre-remove: groups validated when an entity is about to be deleted (default to no group)
  27. org.hibernate.validator.group.ddl: groups considered when applying constraints on the database schema (default to Default)
  28. Each property accepts the fully qualified class names of the groups validated separated by a comma (,)

你可能感兴趣的:(javaEE)