繁体   English   中英

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

[英]javax.persistence.PersistenceException: No Persistence provider for EntityManager named customerManager

我是JPA和Hibernate的新手。 在阅读了一些在线资料后,我现在了解了Hibernate是什么以及它如何与JPA一起使用。

现在,我正在尝试运行这个JPA和Hibernate教程 我已经完成了他们在本教程中提到的所有内容。

我没有Oracle DB,只有MySQL。 所以我使用我对JPA和Hibernate的理解对persistence.xml做了一些更改(我不知道它是否正确......对我而言似乎是。)

这是我的persistence.xml

<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="customerManager" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>Customer</class>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.password" value="1234"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
    </properties>
  </persistence-unit>
</persistence>

但我似乎没有得到他们描述的输出。 它给了我:

Customer id before creation:null
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence     provider for EntityManager named customerManager
 at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
 at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
 at CustomerDAO.create(CustomerDAO.java:8)
 at CustomerDAO.main(CustomerDAO.java:22)

任何建议将不胜感激。

更新:

我已经做了要求完成的更改。 但是,仍然得到asme错误行!

他们没有在该教程中提到有关orm.xml的任何内容。 这可能是一个问题!

只是为了完整。 还有另一种情况导致此错误:

缺少META-INF / services / javax.persistence.spi.PersistenceProvider文件。

对于Hibernate,它位于hibernate-entitymanager-XXX.jar ,因此,如果hibernate-entitymanager-XXX.jar不在您的类路径中,您也会遇到此错误。

此错误消息是如此误导,并且它花费我几个小时才能使其正确。

请参阅使用Hibernate作为提供程序的JPA 2.0 - 例外:EntityManager没有持久性提供程序

您的persistence.xml无效,无法创建EntityManagerFactory 它应该是:

<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="customerManager" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>Customer</class>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.password" value="1234"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/general"/>
      <property name="hibernate.max_fetch_depth" value="3"/>
    </properties>
  </persistence-unit>
</persistence>

(注意<property>元素是如何关闭的,它们不应该嵌套)

更新:我完成了教程,您还必须在使用MySQL时更改Id生成策略(因为MySQL不支持序列)。 我建议使用AUTO策略(默认为IDENTITY和MySQL)。 为此,请删除SequenceGenerator注释并更改如下代码:

@Entity
@Table(name="TAB_CUSTOMER")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CUSTOMER_ID", precision=0)
    private Long customerId = null;

   ...
}

这应该有所帮助。

PS:您还应该按照建议提供log4j.properties

我今天遇到了同样的问题。 我的persistence.xml位于错误的位置。 我不得不把它放在以下路径中:

project/src/main/resources/META-INF/persistence.xml

我面临同样的问题。 我意识到我在persistence.xml中使用了错误的提供程序类

对于Hibernate应该是

<provider>org.hibernate.ejb.HibernatePersistence</provider>

对于EclipseLink应该是

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

如果你使用Hibernate 5.2.10.Final,你应该改变

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

在persistence.xml中

根据Hibernate 5.2.2:EntityManager没有持久性提供程序

如果您使用的是Maven,则可能同时包含 src/{main,test}/resources/META-INF/persistence.xml 这是一个常见的设置:使用h2或Derby测试您的JPA代码,并使用PostgreSQL或其他一些完整的DBMS进行部署。 如果您正在使用此模式,请确保这两个文件具有不同的单元名称,否则Persistence类的某些版本将尝试加载BOTH(因为您的测试时间CLASSPATH包括类和测试类); 这将导致持久性单元的定义冲突,导致我们都讨厌的可怕消息!

更糟糕的是:这可能会与某些旧版本(如Hibernate)“兼容”,但在当前版本中失败。 无论如何都值得一试......

有点太晚了,但是我遇到了同样的问题并修复了它在persistence.xml文件(第1行) 中将schemalocation切换到schemaLocation

我已经看到了这个错误,对我来说问题是persistance.xml的绝对路径中有一个空格,删除它同样帮助了我。

当我试图在Tomcat 8中配置JPA实体管理器时,我也遇到了同样的问题。首先,我遇到了SystemException类未找到的问题,因此没有创建entityManagerFactory。 我删除了hibernate实体管理器依赖项,然后我的entityManagerFactory无法查找持久性提供程序。 经过大量的研究和时间后,我们必须知道hibernate实体管理器必须查找一些配置。 然后放回实体管理器jar,然后添加JTA Api作为依赖项,它工作正常。

我的经验告诉我,缺少persistence.xml,也会生成相同的异常。

我今天遇到了同样的错误信息,当我试图运行由蚂蚁包装的jar包时。

当我使用jar tvf来检查jar文件的内容时,我意识到“ant”忘了为我包装persistnece.xml。

手动重新打包jar文件后,错误信息消失了。

所以我相信也许你应该尝试简单地将META-INF放在src目录下并将你的persistence.xml放在那里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM