javax.persistence.PersistenceException:没有名为sample的EntityManager的持久性提供程序

时间:2011-02-22 06:36:25

标签: hibernate jpa maven entitymanager

我正在使用Hibernate Entity Manager。它抱怨它无法找到持久性提供程序,但仍将其记录在日志中:

    TRACE 2011-02-21 22:17:52,843 [main](Ejb3Configuration.java:321) org.hibernate.ejb.Ejb3Configuration - PersistenceMetadata(version=2.0) [
        name: sample
        jtaDataSource: null
        nonJtaDataSource: null
        transactionType: RESOURCE_LOCAL
        provider: org.hibernate.ejb.HibernatePeristence
        useQuotedIdentifiers: false
        classes[
        ]
        packages[
        ]
        mappingFiles[
        ]
        jarFiles[
        ]
        hbmfiles: 0
        properties[
            hibernate.connection.username: scott
            hibernate.connection.password: tiger
            hibernate.dialect: org.hibernate.dialect.OracleDialect
            hibernate.show_sql: true
            hibernate.connection.url: jdbc:oracle:thin:@localhost:1521:krsna
            hibernate.archive.autodetection: class, hbm
            hibernate.connection.driver_class: oracle.jdbc.driver.OracleDriver
        ]]
    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named sample
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
        at EMPersistDemo2.main(EMPersistDemo2.java:26)

很明显,它找到它,记录它......但仍然抛出异常。 我错过了什么吗?

我正在使用maven,我将persistence.xml放在src / main / resources / META-INF中。 其内容如下:

    <!-- persistence.xml -->
    <persistence ...>
       <persistence-unit name="sample">
        <provider>org.hibernate.ejb.HibernatePeristence</provider>
        <!-- Scan for annotated classes and Hibernate mapping XML files -->
        <properties>
           <property name="hibernate.archive.autodetection" value="class, hbm"/>
           <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
           <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:krsna"/>
           <property name="hibernate.connection.username" value="scott"/>
           <property name="hibernate.connection.password" value="tiger"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
           <property name="hibernate.show_sql" value="true"/>
        </properties>
        </persistence-unit>

    </persistence>

org.hibernate.Ejb3Configuration能够找到它javax.persitence.Persistence无法找到它:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("sample");

3 个答案:

答案 0 :(得分:3)

我也面临同样的问题,但我得到了一个解决方案:缺少META-INF / services / javax.persistence.spi.PersistenceProvider文件。对于hibernate,它位于hibernate-entitymanager-XXX.jar中,因此,如果hibernate-entitymanager-XXX.jar不在你的类路径中,你也会遇到这个错误。

答案 1 :(得分:2)

我在java coderanch上找到了我的问题的答案。

persistence.xml中我的持久性提供程序名称中有一个拼写错误:

我将org.hibernate.ejb.HibernatePeristence重命名为

org.hibernate.ejb.HibernatePersistence
                              ^---

它开始工作了。

我发布了我的答案,以便这对其他人有所帮助。

答案 2 :(得分:0)

闻起来像hibernate jar文件不在类路径中,因为错误消息显示“找不到持久性提供程序”