【问题标题】:No persistence provider for EntityManager, JPA configurationEntityManager 没有持久化提供程序,JPA 配置
【发布时间】:2014-02-22 13:43:30
【问题描述】:

我尝试使用以下 persistence.xml 配置 JPA Hibernate:

<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="manager1" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <mapping-file>orm.xml</mapping-file>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="x" />
        <property name="javax.persistence.jdbc.url"
            value="x;create=true" />
        <property name="javax.persistence.jdbc.user" value="x" />
        <property name="javax.persistence.jdbc.password" value="x" />
        <property name="dialect" value="org.hibernate.dialect.DB2Dialect" />
        <!-- EclipseLink should create the database schema automatically -->
        <!-- <property name="eclipselink.ddl-generation" value="create-tables" 
            /> <property name="eclipselink.ddl-generation.output-mode" value="database" 
            /> -->
    </properties>

</persistence-unit>

在“x”处有合适的值,但我得到了:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at de.volkswagen.webpromet.Main.main(Main.java:10)

感谢您的帮助! ! !

【问题讨论】:

标签: java hibernate jakarta-ee jpa


【解决方案1】:

它是明确显示应该放置persistence.xml的地方

提示:文档是最好的教程!

src
|-- main
|   |-- java
|   `-- resources
|       |-- jpa
|       |   |-- Clerk-orm.xml
|       |   |-- Customer-orm.xml
|       |   |-- Person-orm.xml
|       |   `-- Sale-orm.xml
|       `-- META-INF
|           `-- persistence.xml
`-- test
    `-- resources
        `-- hibernate.properties

更多:http://webdev.apl.jhu.edu/~jcs/ejava-javaee/coursedocs/605-784-site/docs/content/html/hibernate-migration-orm.html#hibernate-migration-orm-mapping

【讨论】:

  • 解决我问题的一种解决方案。谢谢你,朋友。
【解决方案2】:

在 META-INF 文件夹中添加您的 persistence.xml 并像这样创建持久性:

    <persistence version="2.0"
        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">
        <persistence-unit name="abc">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- map your classes. -->
    <properties>
    <property name="javax.persistence.jdbc.driver" value="x" />
    <property name="javax.persistence.jdbc.url"value="x/>
    <property name="javax.persistence.jdbc.user" value="x" />
    <property name="javax.persistence.jdbc.password" value="x" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    <property name="hibernate.show_sql" value="true" />
    </properties>
        </persistence-unit>
    </persistence>

创建一个Java类HibernateUtil.java并将持久化单元名称提供给entityManagerFactory = Persistence.createEntityManagerFactory("abc");

public class HibernateUtil {

    private static final EntityManagerFactory entityManagerFactory;
    static {
                try {
                     entityManagerFactory = Persistence.createEntityManagerFactory("abc");
                     System.out.println("Entity Menager Test.............."+ entityManagerFactory);
                } catch (Throwable ex) {

                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);

                  }
    }

public static EntityManagerFactory getEntityManagerFactory() {
         return entityManagerFactory;
    }

}

试试吧,它会起作用的。

【讨论】:

    【解决方案3】:

    如果您将persistence.xml 文件放在WEB-INF 目录中,它永远不会进入类路径。将META-INF/persistence.xml 文件添加到源文件夹的根目录。

    【讨论】:

      【解决方案4】:

      您需要将persistence.xml 文件放在适当的位置。 在您的情况下,将 META-INF/persistence.xml 文件添加到源文件夹的根目录。

      以下内容来自 JPA 规范

      A persistence.xml file defines a persistence unit. The persistence.xml file is 
      located in the META-INF directory of the root of the persistence unit. 
      

      持久化单元的根是这里的关键。

      由于您使用的是 Java EE 应用程序,因此以下内容有效

      In Java EE environments, the root of a persistence unit must be one of the following:
      • an EJB-JAR file
      • the WEB-INF/classes directory of a WAR file[80]
      • a jar file in the WEB-INF/lib directory of a WAR file
      • a jar file in the EAR library directory
      • an application client jar file
      

      但是,如果您正在创建一个非 Java EE 应用,则以下内容适用:

      The jar file or directory whose META-INF directory contains the persistence.xml 
      file is termed the root of the persistence unit.
      

      【讨论】:

        猜你喜欢
        • 2014-12-15
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-20
        • 1970-01-01
        • 2019-01-16
        相关资源
        最近更新 更多