JPA 2.0:在 JPA 2.0 中使用 javax.validation.* 包的例外情况
Posted
技术标签:
【中文标题】JPA 2.0:在 JPA 2.0 中使用 javax.validation.* 包的例外情况【英文标题】:JPA 2.0 : Exception to use javax.validation.* package in JPA 2.0 【发布时间】:2013-01-21 17:24:46 【问题描述】:当我尝试使用休眠的 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();
【问题讨论】:
你在哪个版本的应用服务器上使用? 没有应用服务器使用,用corejava实现JPA 您从哪个 jar 中获取注释(例如 @NotNull)?你的类路径上有 jsr 303 实现(例如 Hibernate Validator)吗? 我的类路径 1.validation-api-1.0.0.GA.jar
和 2.hibernate-validator-5.0.0.Alpha2.jar
上有 2 个 jar 文件。
Hibernate Validator 5 确实实现了 bean-validation 1.1(来源 planet.jboss.org/post/…)。所以这些罐子不适合在一起。你需要休眠验证器 4
【参考方案1】:
正如 @Korgen 在 cmets hibernate-validator-5.x.x 中提到的,它与 validation-api-1.0.x 不兼容。这是因为迁移到新规范 JSR-303 -> JSR-349。
有两种方法可以解决这个问题:
1.降级休眠验证器版本(实现JSR-303):
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
2. 如果您不想从休眠验证器 5 移回休眠验证器 4,另一种解决方案是将 javax.validation 升级到更高版本(描述 JSR-349):
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
【讨论】:
如果 maven 构建休眠验证器 5 已经有依赖验证-api 1.1.0.Final。无需显式添加。 hibernate-validator 4.3.2.Final 太兼容了【参考方案2】:对于那些收到错误 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
。
【讨论】:
你拯救了我的一天以上是关于JPA 2.0:在 JPA 2.0 中使用 javax.validation.* 包的例外情况的主要内容,如果未能解决你的问题,请参考以下文章