本文介绍了javax.persistence.PersistenceException:没有名为 customerManager 的 EntityManager 的持久性提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to JPA & Hibernate. After reading some online materials I now understand what Hibernate is and how it can be used with JPA.

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

Now, I am trying to run this JPA & Hibernate tutorial. I've done everything they mention in this tutorial.

我没有 Oracle DB,只有 MySQL.所以我利用我对 JPA & 的理解对 persistence.xml 做了一些修改.Hibernate(我不知道它是否正确......在我看来它是.)

I don't have Oracle DB, only MySQL. So I made some changes to persistence.xml using my understanding of JPA & Hibernate (I don't know if it's correct or not... Seems to me it is.)

这是我的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>

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

But I don't seem to get the output they describe. It's giving me:

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 错误行!!!

I have made the changes that are asked to done. But, still getting the asme error lines!!!

他们在那个教程中没有提到任何关于 orm.xml 的内容.可能是问题原因!!!

They didnt mentioned anything about orm.xml in that tutorial. may it be a problem causer!!!

推荐答案

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

Your persistence.xml is not valid and the EntityManagerFactory can't get created. It should be:

<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>

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

(Note how the <property> elements are closed, they shouldn't be nested)

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

Update: I went through the tutorial and you will also have to change the Id generation strategy when using MySQL (as MySQL doesn't support sequences). I suggest using the AUTO strategy (defaults to IDENTITY with MySQL). To do so, remove the SequenceGenerator annotation and change the code like this:

@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.

PS: you should also provide a log4j.properties as suggested.

这篇关于javax.persistence.PersistenceException:没有名为 customerManager 的 EntityManager 的持久性提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:31