JPA -Hibernate教程:显式持久性提供程序错误 - 无法创建配置

时间:2013-01-19 18:56:45

标签: java hibernate jpa

我正在尝试用本网站提到的一个非常简单的例子来学习JPA-Hibernate https://schuchert.wikispaces.com/JPA+Tutorial+1+-+Getting+Started (我也尝试过替代链接,但最终都遇到了同样的问题)

在我简要描述我的例子之前,我列出了重要的例外情况 -

Explicit persistence provider error(s) occurred for "examplePersistenceUnit" after trying the following discovered implementations: org.hibernate.ejb.HibernatePersistence from provider: org.hibernate.ejb.HibernatePersistence
Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
Caused by: javax.validation.ValidationException: Could not create Configuration.
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:175)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

项目配置如下 persistence.xml {在src文件夹目录中META-INF}

<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="examplePersistenceUnit" 
                      transaction-type="RESOURCE_LOCAL">     
       <properties>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />

            <property name="hibernate.connection.driver_class" 
                      value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.connection.url" 
                      value="jdbc:hsqldb:mem:mem:aname" />
            <property name="hibernate.connection.username" value="sa" />

            <property name="hibernate.dialect" 
                      value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>
</persistence>

然后我有一个简单的junit类,它执行以下操作

@Before
    public void init(){
         BasicConfigurator.configure();
            Logger.getLogger("org").setLevel(Level.TRACE);
        emf= Persistence.createEntityManagerFactory("examplePersistenceUnit");
        em = emf.createEntityManager();

    }

任何人都可以帮我解决这个问题我尝试在论坛上搜索并使用反编译器进行调试但没有用

完整异常跟踪

javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for "examplePersistenceUnit" after trying the following discovered implementations: org.hibernate.ejb.HibernatePersistence from provider: org.hibernate.ejb.HibernatePersistence
    at javax.persistence.Persistence.createPersistenceException(Persistence.java:244)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:186)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:72)
    at entity.PersonTest.init(PersonTest.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: examplePersistenceUnit] 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:152)
    ... 26 more
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: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)
    ... 29 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:208)
    ... 34 more
Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:524)
    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119)
    ... 39 more
Caused by: javax.validation.ValidationException: Could not create Configuration.
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:175)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:50)
    at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521)
    ... 40 more
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:571)
    at java.util.ArrayList.get(ArrayList.java:349)
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:173)
    ... 42 more

1 个答案:

答案 0 :(得分:2)

你的类路径中是否有Hibernate验证器?

如果您使用的是Maven,只需为相应的Hibernate版本()添加dependency