JPA exception: entity missing provider for EntityManager named MyJPAApplicationPU

I am new to JPA. I try to run the sample code using JPA, but I get the following exception:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU

I posted my exception message here,

INFO: Could not find any META-INF/persistence.xml file in the classpath
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
        at com.myJpa.TestJPA.setUp(TestJPA.java:30)
        at com.myJpa.TestJPA.main(TestJPA.java:72)

Any help would be appreciated.

+3
source share
3 answers

Well, the error is self-explanatory, you need to provide META-INF/persistence.xmlJPA to use. This file is used to define the "storage unit". From the JPA 1.0 specification:

6.2.1 persistence.xml file

A persistence.xml . , / factory . persistence.xml META-INF . , .

persistence.xml Java SE ( Hibernate JPA):

<?xml version="1.0" encoding="UTF-8"?>
<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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.mycompany.Foo</class>
        <class>com.mycompany.Bar</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

:

  • Java SE JTA, RESOURCE_LOCAL ( Java SE, ).
  • Java SE JDNI, JDBC, ( , URL- , , ). JPA 1.0 , , .
  • Java SE, , .
+5

JPA META-INF/persistence.xml. , -, WEB-INF/classes/.

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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">

  </persistence-unit>

</persistence>
+5
 EntityManagerFactory emf = Persistence.createEntityManagerFactory("<JDBC connection>");

Verify the correct JDBC connection.

0
source

Source: https://habr.com/ru/post/1754024/


All Articles