Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Bean Validation not available in the class path

I decided to try out Hibernate today, which looks promising, although the setup could be easier. After having found solutions for the first million or so configuration errors I'm now stuck with this one:

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>

The (as far as I can tell) relevant portions of 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 have tried adding this to the build path without any change. I have googled for Bean Validation not available in the class path but required in javax.persistence.validation.mode and also just Bean Validation not available in the class path. I found the source code, but that doesn't help me much.

I have also tried removing the properties from the xml configuration files, without any luck.

I do not use Maven. I simply 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 additional jar in my build path that I have missed? (In that case which one?) and also: Why is it needed? Can I change something so that it is not needed anymore?

like image 705
Simon Forsberg Avatar asked Dec 06 '25 17:12

Simon Forsberg


1 Answers

You need to have the validation-api (there's a download JAR link you can use as you don't use maven) in your classpath.

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

  • If you're not using maven you need to build up the classpath yourself and it's easy to miss a jar. It might be worthwhile to just use maven to bootstrap your project, and to make sure you have all the jars in place.
  • If you're using maven, the validation-api is pulled in as a transitive dependency from hibernate-validator.
like image 159
ddewaele Avatar answered Dec 08 '25 06:12

ddewaele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!