Home > Net >  Exception while using Jakarta JPA 3.0
Exception while using Jakarta JPA 3.0

Time:02-23

I am getting an exception on a project based on Jakarta JPA 3.0. Below is my dependency, persistence.xml and exceptionfile.

Dependency

<groupId>jakarta.persistence</groupId>
  <artifactId>jakarta.persistence-api</artifactId>
  <version>3.0.0</version>
</dependency>  

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
    version="3.0">
    <persistence-unit name="usecase" transaction-type="RESOURCE_LOCAL">
        <exclude-unlisted-classes>false</exclude-unlisted-classes>      
        <properties>
            <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/usecase" />
            <property name="jakarta.persistence.jdbc.user" value="root" />
            <property name="jakarta.persistence.jdbc.password" value="********" />
        </properties>               
    </persistence-unit>
</persistence>

Exception

Feb 16, 2022 1:39:01 PM jakarta.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver log
WARNING: jakarta.persistence.spi::No valid providers found.
Exception in thread "main" java.lang.ExceptionInInitializerError
    at za.co.lot24media.usecase.Main.main(Main.java:10)
Caused by: jakarta.persistence.PersistenceException: No Persistence provider for EntityManager named usecase
    at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:86)
    at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at za.co.lot24media.usecase.Database.<init>(Database.java:15)
    at za.co.lot24media.usecase.Database.<clinit>(Database.java:9)
    ... 1 more

CodePudding user response:

You only have a dependency towards the Java Persistence API. However, to use JPA 3.0 in your application, you need to add a dependency towards an implementation, e.g. to EclipseLink.

In order to use EclipseLink as JPA provider, you have to add the related implementation as a dependency to your project, e.g. via Maven:

<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>3.0.2</version>
</dependency>

Note, that you need a version > 3.0.0 in order to use Jakarta Persistence API 3.0.

The provider can then be specified in the persistence.xml via the <provider>-tag (see the documentation of EclipseLink for more information):

<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence
    https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
             version="3.0">
    <persistence-unit name="ejblite-pu" transaction-type="JTA">
     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    </persistence-unit>
</persistence>
  • Related