Hibernate Bean Validation not available on classpath

I decided to give Hibernate a try today, which looks promising, although the setup might be easier. Having found solutions for the first million configuration errors, I am now stuck with this:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: manager1] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
(...)
Caused by: org.hibernate.HibernateException: Bean Validation not available in the class path but required in javax.persistence.validation.mode
    at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyHibernateListeners(BeanValidationIntegrator.java:281)
    at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:134)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:303)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
    ... 5 more

      

Peristence.xml:

<?xml version='1.0' encoding='utf-8' ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
   <persistence-unit name="manager1" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>net.zomis.hibernate.Game</class>
      <class>net.zomis.hibernate.TestFields</class>
      <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
      <validation-mode>CALLBACK</validation-mode>
      <properties>
          <property name="javax.persistence.validation.mode" value="NONE" />  
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="validate"/>
      </properties>
   </persistence-unit>
</persistence>

      

(as far as I can tell) the relevant parts hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        (...)
        <property name="hbm2ddl.auto" >validate</property>
        <mapping class="net.zomis.hibernate.Game" />
        <mapping class="net.zomis.hibernate.TestFields" />
    </session-factory>
</hibernate-configuration>

      

I tried adding this to the build path without any changes. I have googled for Bean Validation not available in the class path but required in javax.persistence.validation.mode

as well as simple Bean Validation not available in the class path

. I found the source code, but it doesn't help much.

I have also tried removing properties from xml config files with no luck.

I am not using using Maven. I just use Eclipse and build and run the project directly from Eclipse.

The obvious question is, how can I solve this problem? Do I need an extra jug in my build path that I missed? (In which case?), And also: why is it necessary? Can I change something so that it is no longer needed?

+3


source to share


2 answers


You need to have validation-api (there is a link to download the JAR that you can use as you are not using maven) in your classpath.

Hibernate looks for the javax.validation.Validation class on the classpath. If it doesn't find it, it throws an error Bean Validation not available in the class path but required in javax.persistence.validation.mode

.



  • If you are not using maven you need to create the classpath yourself and can easily skip the jar. It might be worth just using maven to load your project and make sure you have all the jars in place.
  • If you're using maven, the validation-api is pulled as a transitive dependency on the hibernate-validator.
+6


source


You need the Hibernate validator artifact:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.3.1.Final</version>
</dependency>

      

This will also include the Bean Validation API, which is indeed a requirement in this case.

If you are not using any Bean validation constraints (like @NotNull, @Before, @Pattern, etc.) you can also just remove



<validation-mode>CALLBACK</validation-mode>

      

or set it to NONE. Bean Validation is not required to use Hibernate, it is just an addon defined in the JPA spec. If Bean Validation is on class lifecycle validation (entity validation on pre-refresh, pre-save, etc.) Automatically enabled (see also http://docs.jboss.org/hibernate/stable/validator/reference/ en-US / html_single / # d0e3096 and of course the JPA spec 2).

On the other hand, if you don't have Bean Validation on the classpath, but explicitly ask for it using a config option in the persistence.xml file, you will get the error you described.

+2


source







All Articles