hibernateを使用してJPAでBean検証を使用しようとすると、次の例外が発生します:
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
私は検証パッケージを使用するのが初めてで、Java EE Beanの知識がありません。この検証パッケージをjpaエンティティで使用する方法は次のコードです:
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();
@Korgenがコメントで言及したようにhibernate-validator-5.xxはvalidation-api-1.0と互換性がありません.x。これは、新しい仕様JSR-303-> JSR-349に移行したためです。
この問題を解決するには2つの方法があります。
1。 hibernate検証バージョンのダウングレード(JSR-303を実装):
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
2。 hibernateバリデーター5からhibernateバリデーター4に戻りたくない場合、別の解決策はjavax.validationをより高いバージョン(JSR-349について説明)にアップグレードすることです。
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
エラーNoClassDefFoundError: javax/validation/valueextraction/ValueExtractorDeclarationException
を取得し、検証APIが既にjavax.validation:validation-api:1.1.0.Final
であった人向け
修正は、javax.validation:validation-api:1.1.0.Final
からjavax.validation:validation-api:2.0.1.Final
にアップグレードすることでした。