简体   繁体   中英

No persistence provider for entitymanager named xyz

I have a JavaFX Maven project in IntelliJ, which uses Hibernate. When the app is starting, I am getting the following error message:

No persistence provider for entitymanager named xyz

Why? My META-INF/persistence.xml is located in myproject/src/main/resources (the directory is checked as resource folder in project settings). I am sure I've downloaded all Hibernate JARs via Maven.

My persistence.xml is correct:

<?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
      <persistence-unit name="xyz">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>entity.User</class>
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect"/>
          <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:java.sqlite"/>
          <property name="javax.persistence.jdbc.driver_class" value="oracle.jdbc.driver"/>
          <property name="hibernate.hbm2ddl.auto" value="none"/>
          <property name="hibernate.show_sql" value="false"/>
          <property name="javax.persistence.schema-generation.database.action" value="create"/>
        </properties>
      </persistence-unit>
    </persistence>

Java code:

factory = Persistence.createEntityManagerFactory("xyz");

In my an old non-Maven project the code above worked fine.

您的persistence.xml必须位于META-INF文件夹中

There was a different context and solution in my case:

Context:

see the log below:

org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:tcp://localhost/~/test]
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa}
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
...
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named NewPersistenceUnit`

Solution:

As its clear from logs above, persistenceUnit has been found and some data like dialect, username and password is extracted.

There was problem in mapping of entities. By commenting out @Entity one by one on entities, I was able to find the problem and solve it, without any change in persistence.xml file.

I have no idea why error like No Persistence provider for EntityManager named X should be shown when there is a problem in entity mapping!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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