无法使用persistence.xml

时间:2017-12-22 07:21:11

标签: eclipse hibernate jpa java-ee orm

突然间,我使用hibernate将用户插入数据库表的代码停止工作(并且它已经工作了很长时间没有错误)。我得到的例外是它无法创建持久性单元:这里是如下所示的堆栈跟踪:

** --------------------------------------------- --------------------------------

 javax.persistence.PersistenceException: [PersistenceUnit: craiglistPU] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:967)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at utils.JPAUtil.getEntityManagerFactory(JPAUtil.java:15)
    at dao.UserDAOImpl.insertUser(UserDAOImpl.java:38)
    at tests.MainDriver.main(MainDriver.java:21)

** --------------------------------------------- --------------------------------

根文件异常发生在这个文件中,在'if'语句中的行上:

public class JPAUtil {
  //name of persistence unit from persistence.xml
  private static final String PERSISTENCE_UNIT_NAME = "craiglistPU";
  private static EntityManagerFactory factory;

  public static EntityManagerFactory getEntityManagerFactory() {
       if (factory == null) {
       //line below this throws exception
       factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
      //line above this
      }
      return factory;
  }

** --------------------------------------------- -------------------------------------------------- --------- 这是我的数据源设置的屏幕截图,它正确地ping /连接: working datasource setup

最后,这是我的persistence.xml的代码:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
         http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">

<persistence-unit name="craiglistPU" transaction-type="RESOURCE_LOCAL">
    <description>Persistence unit used for craigslist database</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <properties>
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/craigslist?useSSL=false" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="update" />   
    </properties>

</persistence-unit>

我会假设我的bean / model类已正确配置;我使用eclipse jpa来创建它们,并且自创建以来就没有对它们进行修改。

如何解决此问题的任何想法将不胜感激。此外,如果您想要查看更多源代码,请告诉我,我可以提供它(或允许您访问我的存储库)。非常感谢!

EDIT 在检查堆栈跟踪的原因之后:

Caused by: org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:138)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:276)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:490)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:878)
... 6 more
Caused by: java.lang.NoSuchMethodError:     javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String;
at org.hibernate.validator.internal.xml.ValidationBootstrapParameters.<init>(ValidationBootstrapParameters.java:61)
at org.hibernate.validator.internal.engine.ConfigurationImpl.parseValidationXml(ConfigurationImpl.java:486)
at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:295)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:463)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.activate(TypeSafeActivator.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:132)
... 9 more

我现在相信这是由javax.validation和hibernate-validator问题引起的。我的pom包含以下依赖项:

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator-annotation-processor -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>

任何想法,如果我需要从这些依赖项中排除任何内容,或选择不同的版本号,以便它们一起正常工作?感谢。

1 个答案:

答案 0 :(得分:0)

NoSuchMethodError表明版本不匹配。 The documentation说丢失的方法在2.0 javax.validation.validation-api库中使用,所以也许你的类路径中加载了一个冲突的旧版本?你可以用maven检查dependency tree以检查正在加载的jar的旧版本吗?