我在基于 Jakarta JPA 3.0 的專案中遇到例外。下面是我的依賴,persistence.xml 和例外檔案。
依賴
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
</dependency>
持久性.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>
例外
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
uj5u.com熱心網友回復:
您只依賴于 Java Persistence API。但是,要在您的應用程式中使用 JPA 3.0,您需要添加對實作的依賴項,例如 EclipseLink。
為了使用 EclipseLink 作為 JPA 提供者,您必須將相關實作作為依賴項添加到您的專案中,例如通過 Maven:
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>3.0.2</version>
</dependency>
請注意,您需要版本 > 3.0.0 才能使用 Jakarta Persistence API 3.0。
然后可以persistence.xml通過<provider>-tag 在 中指定提供程式(有關更多資訊,請參閱 EclipseLink的檔案):
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/432639.html
上一篇:內部聯接應該在JPA中留下聯接
