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

20 java orm hibernate jpa

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

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

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

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激.

更新:

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

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

tur*_*ove 25

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

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

对于Hibernate,它位于hibernate-entitymanager-XXX.jar,所以,如果hibernate-entitymanager-XXX.jar不在你的类路径中,你也会遇到这个错误.

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

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


Pas*_*ent 19

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>
Run Code Online (Sandbox Code Playgroud)

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

更新:我完成了教程,你还必须Id在使用MySQL时改变生成策略(因为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;

   ...
}
Run Code Online (Sandbox Code Playgroud)

这应该有所帮助.

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


Dan*_*rey 17

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

project/src/main/resources/META-INF/persistence.xml
Run Code Online (Sandbox Code Playgroud)


小智 10

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

对于Hibernate应该是

<provider>org.hibernate.ejb.HibernatePersistence</provider>
Run Code Online (Sandbox Code Playgroud)

对于EclipseLink应该是

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
Run Code Online (Sandbox Code Playgroud)


Jus*_*tas 5

如果您使用 Hibernate 5.2.10.Final,您应该更改

<provider>org.hibernate.ejb.HibernatePersistence</provider>
Run Code Online (Sandbox Code Playgroud)

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
Run Code Online (Sandbox Code Playgroud)

在你的 persistence.xml 中

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