gpt4 book ai didi

java - 找不到类 : javax. persistence.spi.ProviderUtil

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:09 26 4
gpt4 key购买 nike

我正在尝试使用 JPA 设置一个简单的 Spring Web 应用程序,但在运行 mvn spring-boot:run 后,我得到了以下堆栈跟踪。当然,我尝试搜索,发现该类不在“类路径”上,但并不真正知道类路径在这种情况下意味着什么,以及如何向其中添加某些内容,以及为什么需要它。因此,提供一点帮助就太好了。

[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:506)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.newInstance(Class.java:412)
at javax.persistence.Persistence.findAllProviders(Persistence.java:112)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
at hu.creativetribes.sga.configurations.PersistenceManager.<init>(PersistenceManager.java:13)
at hu.creativetribes.sga.configurations.PersistenceManager.<clinit>(PersistenceManager.java:8)
at hu.creativetribes.sga.Application.main(Application.java:18)
... 6 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.spi.ProviderUtil
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more

这是我的主要方法,它是持久化对象的测试:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);

Partner partner = new Partner("test", "test", "test", "test", "test", "test");

EntityManager em = PersistenceManager.INSTANCE.getEntityManager();
em.getTransaction().begin();
em.persist(partner);
em.getTransaction().commit();
em.close();
PersistenceManager.INSTANCE.close();

}

}

持久化管理器枚举:

public enum PersistenceManager {
INSTANCE;

private EntityManagerFactory emFactory;

private PersistenceManager() {
emFactory = Persistence.createEntityManagerFactory("sga-jpa");
}

public EntityManager getEntityManager() {
return emFactory.createEntityManager();
}

public void close() {
emFactory.close();
}

}

我的 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="sga-jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/sg_be" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />

<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />
</properties>
</persistence-unit>
</persistence>

最后是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">


<modelVersion>4.0.0</modelVersion>
<groupId>stackoverflow_heplme</groupId>
<artifactId>sga</artifactId>
<packaging>war</packaging>
<version>1.0-VIVALDI</version> <!-- Haha, spring. -->

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<properties>
<java-version>1.8</java-version>
<hibernate.version>4.3.6.Final</hibernate.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>com.google.appengine.orm</groupId>
<artifactId>datanucleus-appengine</artifactId>
<version>2.1.2</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

</dependencies>


<build>
<finalName>sga</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>

</configuration>
</plugin>
</plugins>
</build>

</project>

最佳答案

首先,如果您想使用hibernate,则不需要创建entityManager,因为它随springboot bundle 一起提供,并且持久化提供程序在依赖项下生成背景,因此您也不需要persistence.xml,您可以在pom.xml和application.properties上进行管理。

org.springframework.boot spring-boot-starter-data-jpa

将此添加到您的 pom.xml

关于java - 找不到类 : javax. persistence.spi.ProviderUtil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45224272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com