No Persistence provider for EntityManager named

前端 未结 30 1986
甜味超标
甜味超标 2020-11-22 03:45

I have my persistence.xml with the same name using TopLink under the META-INF directory. Then, I have my code calling it with:

相关标签:
30条回答
  • 2020-11-22 04:40

    Try also copying the persistence.xml manually to the folder <project root>\bin\META-INF. This fixed the problem in Eclipse Neon with EclipseLink 2.5.2 using a simple plug-in project.

    0 讨论(0)
  • 2020-11-22 04:41

    If there are different names in Persistence.createEntityManagerFactory("JPAService") in different classes than you get the error. By refactoring it is possible to get different names which was in my case. In one class the auto-generated Persistence.createEntityManagerFactory("JPAService")in private void initComponents(), ContactsTable class differed from Persistence.createEntityManagerFactory("JPAServiceExtended") in DBManager class.

    0 讨论(0)
  • 2020-11-22 04:42

    I needed this in my pom.xml file:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.6.Final</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-22 04:42

    Make sure you have created persistence.xml file under the 'src' folder. I created under the project folder and that was my problem.

    0 讨论(0)
  • 2020-11-22 04:42

    It happenes when the entity manager is trying to point to many persistence units. Do the following steps:

    1. open the related file in your editor (provided your project has been closed in your IDE)
    2. delete all the persistence and entity manager related code
    3. save the file
    4. open the project in your IDE
    5. now bind the db or table of your choice
    0 讨论(0)
  • 2020-11-22 04:44

    I'm some years late to the party here but I hit the same exception while trying to get Hibernate 3.5.1 working with HSQLDB and a desktop JavaFX program. I got it to work with the help of this thread and a lot of trial and error. It seems you get this error for a whole variety of problems:

    No Persistence provider for EntityManager named mick
    

    I tried building the hibernate tutorial examples but because I was using Java 10 I wasn't able to get them to build and run easily. I gave up on that, not really wanting to waste time fixing its problems. Setting up a module-info.java file (Jigsaw) is another hairball many people haven't discovered yet.

    Somewhat confusing is that these (below) were the only two files I needed in my build.gradle file. The Hibernate documentation isn't clear about exactly which Jars you need to include. Entity-manager was causing confusion and is no longer required in the latest Hibernate version, and neither is javax.persistence-api. Note, I'm using Java 10 here so I had to include the jaxb-api, to get around some xml-bind errors, as well as add an entry for the java persistence module in my module-info.java file.

    Build.gradle

    // https://mvnrepository.com/artifact/org.hibernate/hibernate-core
    compile('org.hibernate:hibernate-core:5.3.1.Final')
    
    // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
    

    Module-info.java

    // Used for HsqlDB - add the hibernate-core jar to build.gradle too
    requires java.persistence;
    

    With hibernate 5.3.1 you don't need to specify the provider, below, in your persistence.xml file. If one is not provided the Hibernate provider is chosen by default.

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

    The persistence.xml file should be located in the correct directory so:

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

    Stepping through the hibernate source code in the Intellij debugger, where it checks for a dialect, also threw the exact same exception, because of a missing dialect property in the persistence.xml file. I added this (add the correct one for your DB type):

    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
    

    I still got the same exception after this, so stepping through the debugger again in Intellij revealed the test entity I was trying to persist (simple parent-child example) had missing annotations for the OneToMany, ManyToOne relationships. I fixed this and the exception went away and my entities were persisted ok.

    Here's my full final persistence.xml:

    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    
    <persistence-unit name="mick" transaction-type="RESOURCE_LOCAL">
        <description>
            Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
        </description>
    
        <!-- Provided in latest release of hibernate
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        -->
    
        <class>com.micks.scenebuilderdemo.database.Parent</class>
        <class>com.micks.scenebuilderdemo.database.Child</class>
    
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver"/>
    
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:hsqldb:file:./database/database;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>
    
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>
    
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        </properties>
    
    </persistence-unit>
    
    </persistence>
    

    I probably wasted about half a day on this gem. My advice would be to start very simple - a single test entity with one or two fields, as it seems like this exception can have many causes.

    0 讨论(0)
提交回复
热议问题