【问题标题】:No Persistence provider for "EntityManager" named "firstOne" Java没有名为“firstOne”Java 的“EntityManager”的持久性提供程序
【发布时间】:2014-01-05 05:21:01
【问题描述】:

我正在使用persistense api连接数据库

这是 pom.xml 依赖项

 <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.8.Final</version>
  </dependency>
  <dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
  </dependency>
  <dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>

persistense.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
    version="1.0">
    <persistence-unit name="firstOne">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.ejb.cfgfile" value="/META-INF/hibernate.cfg.xml" />
        </properties>
    </persistence-unit>
</persistence>

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  <hibernate-configuration>
<session-factory>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">tauren993</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="hibernate.generate_statistics">false</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hibernate.cache.use_structured_entries">false</property>
    <property name="hibernate.cache.use_second_level_cache">false</property>

    <mapping class="Employee" />
</session-factory>
</hibernate-configuration>

hibernate.cfg.xml 和 persistense.xml 存储在 project/META-INF 文件夹中,我收到错误

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

堆栈跟踪:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named firstOne
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at Main.main(Main.java:14)

【问题讨论】:

  • 请提供错误的完整堆栈跟踪。
  • javax.persistence.PersistenceException:在 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) 的 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) 中没有名为 firstOne 的 EntityManager 的持久性提供程序) 在 Main.main(Main.java:14)

标签: java xml hibernate maven


【解决方案1】:

在您的持久性单元“firstOne”中,您应该包括提供者。像下面这样说:

<persistence-unit name="firstOne">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>

【讨论】:

【解决方案2】:

您可能错过了实体声明。如果您在 JAVA EE 上下文中,请确保您的项目中有一些类被注释为实体。否则,在 JAVA SE 上下文中,您必须按照 thiyaga 所说的方式定义提供者(请参阅 doc)并指定实体如下:

<persistence-unit name="OrderManagement">
    [...]
    <jar-file>MyOrderApp.jar</jar-file>
     or / and 
    <class>com.widgets.Order</class>
    <class>com.widgets.Customer</class>
</persistence-unit>

此外,

如果可以的话,你应该使用 JPA 2

您不再需要hibernate.cfg.xml 文件,只需将您的属性直接放在persitence.xml 文件的&lt;properties&gt; 元素中,确保hibernate 属性以hibernate 为前缀。

【讨论】:

  • 在持久化上下文初始化期间,您应该会在服务器启动日志中看到错误
  • 在 SE 上下文中,您可以添加 hibernate.archive.autodetectionproperty,如 Pascal Thivent 所述stackoverflow.com/questions/1780341/…
猜你喜欢
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 2015-11-08
  • 2016-04-18
  • 1970-01-01
  • 2019-12-04
  • 2014-03-03
相关资源
最近更新 更多