【问题标题】:Hibernate, JPA, No Persistence provider for EntityManager namedHibernate, JPA, No Persistence provider for EntityManager named
【发布时间】:2015-07-21 23:16:35
【问题描述】:

我正在使用 JPA 和 Hibernate 开发 Swing 应用程序。但是每次我尝试调用以下代码来获取EntityManager

try {
    Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
EntityManagerFactory emf = Persistence.createEntityManagerFactory("abcd");
EntityManager em = emf.createEntityManager();

我得到以下异常:

Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named abcd: Provider named org.hibernate.jpa.HibernatePersistenceProvider threw unexpected exception at create EntityManagerFactory: 
javax.persistence.PersistenceException
javax.persistence.PersistenceException: Unable to build entity manager factory

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

persistence.xml 的路径是 my.jar/meta-inf/persistence.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="abcd" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.url" value="xxx"/>
            <property name="hibernate.connection.password" value="xxx"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.user" value="xxx"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        </properties>
    </persistence-unit>
</persistence>

依赖关系:

除了junit之外的所有依赖都在一个类路径中

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.10.Final</version>
        <scope>compile</scope>
    </dependency>

【问题讨论】:

  • 请检查类 org.hibernate.ejb.Hibernat ePersistence 是否在您的类路径中。

标签: swing hibernate jpa persistence dialect


【解决方案1】:

它似乎找不到您的persistence.xml 文件。 persistence.xml 文件的正确文件夹是 META-INF 而不是 meta-inf

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 2014-09-30
    • 2023-02-26
    • 1970-01-01
    相关资源
    最近更新 更多