51

when i try to using bean validation with JPA using hibernate , the follwoing exception will occur :

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: Chapter11] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at example.test.Test.main(Test.java:18)

Caused by: org.hibernate.HibernateException: Error applying BeanValidation relational constraints
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
... 5 more

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:208)
... 10 more
Caused by: java.lang.NoClassDefFoundError: javax/validation/ParameterNameProvider
at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:41)
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119)
... 15 more

Caused by: java.lang.ClassNotFoundException: javax.validation.ParameterNameProvider
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 20 more

i am new to use validation package and i have not knowledge of JAVA EE beans . how to use this validation package with jpa entity following is my code :

 public class Employee implements Serializable , EmployeeDetail{


private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
@NotNull
private String name;
    ____________________

public class Test {
public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Chapter11");
    EntityManager em = emf.createEntityManager();
    EntityTransaction etr = em.getTransaction();

    etr.begin();
            ------------------

        FullTimeEmployee flEmp = new FullTimeEmployee();
        flEmp.setName("Harmeet Singh");
        flEmp.setPension(56000);
        flEmp.setSalary(100000);

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        validator.validate(flEmp, Employee.class);

        em.persist(flEmp);
    etr.commit();
5
  • Which app server are you using in which version?
    – Korgen
    Commented Feb 6, 2013 at 15:27
  • there is no application server using , implement JPA with corejava Commented Feb 7, 2013 at 9:18
  • from which jar are you getting the annotations (e.g. @NotNull) from? Do you have a jsr 303 implementation (e.g. Hibernate Validator) on your classpath?
    – Korgen
    Commented Feb 7, 2013 at 12:35
  • 1
    i have 2 jar file on my class path 1. validation-api-1.0.0.GA.jar and 2. hibernate-validator-5.0.0.Alpha2.jar . Commented Feb 7, 2013 at 19:11
  • 7
    Hibernate Validator 5 does implement bean-validation 1.1 (source planet.jboss.org/post/…). So these jars do not fit together. You need hibernate validtor 4
    – Korgen
    Commented Feb 12, 2013 at 8:48

2 Answers 2

109

As @Korgen mentioned in comments hibernate-validator-5.x.x isn't compatible with validation-api-1.0.x. This is because of moving to new specification JSR-303 -> JSR-349.

There are two ways to solve this issue:

1. Downgrade hibernate validator version (which is implements JSR-303):

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

2. If you don't want to move back from hibernate validator 5 to hibernate validator 4 another solution is to upgrade javax.validation to higher version (which is describe JSR-349):

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
2
  • 5
    In case of maven build hibernate validator 5 already has dependency validation-api 1.1.0.Final. No need to add it explicitly. Commented Aug 29, 2014 at 13:47
  • hibernate-validator 4.3.2.Final is too comptatible Commented Mar 18, 2016 at 13:01
21

For those who got error NoClassDefFoundError: javax/validation/valueextraction/ValueExtractorDeclarationException and their validation-api was already javax.validation:validation-api:1.1.0.Final

The fix was to upgrade from javax.validation:validation-api:1.1.0.Final to javax.validation:validation-api:2.0.1.Final.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.