wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Can you disable bean validation in Hibernate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can you disable bean validation in Hibernate?" Watch "Can you disable bean validation in Hibernate?" New topic
Author

Can you disable bean validation in Hibernate?

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Hi,

In our project (using Maven) we have a dependency on Hibernate (4.1.0.Final) and a dependency on the JavaEE API 6. If I try to run my unit tests, I get the following error:
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:295)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:115)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:302)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1740)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:88)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 63 more


I know this issue can be solved by adding Hibernate Validator as a test-dependency. If you also use version 4.1.0.Final you'll get another error (class ConfigurationImpl could not be initialized), solved by adding slf4j-api-1.6.1 as a dependency; if you use version 4.3.0.Final of Hibernate Validator instead the unit tests run successfully.


I was wondering if there's a possibility to configure Hibernate (Entity Manager) not to load (look for) Bean Validation classes, because it's not used in the project at all. It just happens to be on the classpath.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

You can try setting the hibernate property javax.persistence.validation.mode to none.



From docs

validation-mode

By default, Bean Validation (and Hibernate Validator) is activated. When an entity is created, updated (and optionally deleted), it is validated before being sent to the database. The database schema generated by Hibernate also reflects the constraints declared on the entity.

You can fine-tune that if needed:

AUTO: if Bean Validation is present in the classpath, CALLBACK and DDL are activated.

CALLBACK: entities are validated on creation, update and deletion. If no Bean Validation provider is present, an exception is raised at initialization time.

DDL: (not standard, see below) database schemas are entities are validated on creation, update and deletion. If no Bean Validation provider is present, an exception is raised at initialization time.

NONE: Bean Validation is not used at all


[How To Ask Questions][Read before you PM me]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Added it to the persistence.xml, still same exception:

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

I think you are stuck then adding an additional test dependency like you originally stated. You can try this one. This in addition to disabling validation should do it.

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Bill Gorder wrote:You can try this one. This in addition to disabling validation should do it.

Unfortunately it doesn't. The property seems just to be ignored by almighty Hibernate.

Exception:
Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:524)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119)
... 79 more
Caused by: javax.validation.ValidationException: Unable to find a default provider
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521)
... 80 more
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Roel De Nijs wrote:The property seems just to be ignored by almighty Hibernate.



Seems that way doesn't it. Try adding this to your persistence xml




Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5154
    
  12

Bill Gorder wrote:Try adding this to your persistence xml

Unfortunately, adding <validation-mode>NONE</validation-mode> to persistence.xml doesn't work either.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you disable bean validation in Hibernate?
 
Similar Threads
gwt spring hibernate validation exception
maven dependency runtime
java.lang.NoClassDefFoundError
Problem in config. JPA in JBOSS
java.lang.ClassNotFoundException: org.apache.axis2.transport.local.LocalTransportSender