1

我正在尝试通过此站点 https://schuchert.wikispaces.com/JPA+Tutorial+1+-+Getting+Started中提到的一个非常简单的示例来学习 JPA-Hibernate (我也尝试过备用链接,但最终都结束了有同样的问题)

在我在这里简要描述我的例子之前,我已经列出了重要的例外 -

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 {inside src folder directory 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
4

1 回答 1

2

Hibernate 验证器是否在您的类路径中?

如果您使用的是 Maven,只需添加相应 Hibernate 版本的依赖项 ()。

于 2013-01-19T19:19:50.930 回答