Error when running DemoApplication.java -dependencies

huangapple go评论77阅读模式
英文:

Error when running DemoApplication.java -dependencies

问题

当我尝试在Maven中运行DemoApplication并使用以下依赖项时,我收到了以下错误。

错误信息:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-04-04T12:29:29.085+03:00 ERROR 13816 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider does not implement the requested interface jakarta.persistence.spi.PersistenceProvider
...

我检查了其他问题和org.springframework.jpa的文档,并添加了它们的依赖项,假设jpa依赖项可能是问题所在。我是Java Spring的新手,所以依赖项可能有点混乱。

英文:

When I try to run the DemoApplication with the following dependencies in maven

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.0-SNAPSHOT</version>
		<relativePath/>
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.12.7.1</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.5.6.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>5.5.6.Final</version>
		</dependency>
		<dependency>
			<groupId>jakarta.persistence</groupId>
			<artifactId>jakarta.persistence-api</artifactId>
			<version>3.0.0</version>
		</dependency>
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.32</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<releases>
				<enabled>false</enabled>
			</releases>
		</pluginRepository>
	</pluginRepositories>

</project>


I get these errors.

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-04-04T12:29:29.085+03:00 ERROR 13816 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider does not implement the requested interface jakarta.persistence.spi.PersistenceProvider
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1132) ~[spring-context-6.0.7.jar:6.0.7]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:907) ~[spring-context-6.0.7.jar:6.0.7]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.7.jar:6.0.7]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.0-20230328.143142-136.jar:3.1.0-SNAPSHOT]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.1.0-20230328.143142-136.jar:3.1.0-SNAPSHOT]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.1.0-20230328.143142-136.jar:3.1.0-SNAPSHOT]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.1.0-20230328.143142-136.jar:3.1.0-SNAPSHOT]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.1.0-20230328.143142-136.jar:3.1.0-SNAPSHOT]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.1.0-20230328.143142-136.jar:3.1.0-SNAPSHOT]
	at com.example.demo.DemoApplication.main(DemoApplication.java:9) ~[classes/:na]
Caused by: java.lang.IncompatibleClassChangeError: Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider does not implement the requested interface jakarta.persistence.spi.PersistenceProvider
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.7.jar:6.0.7]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.7.jar:6.0.7]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.7.jar:6.0.7]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.7.jar:6.0.7]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.7.jar:6.0.7]
	... 16 common frames omitted
Process finished with exit code 1

I checked other questions and the documentation on org.springframework.jpa and I added their dependency assuming that the jpa dependency is the problem.
I am new to java spring so the dependency might be a little messy

答案1

得分: 0

首先,你应该在你的问题中添加更多的描述,比如你正在尝试做什么以及一些代码片段。不过,如果你正在创建Spring Boot应用程序,你应该总是使用starter依赖项。Spring Boot starters可以通过添加一个依赖项来帮助减少手动添加的依赖项数量,并提供强大的功能,即"自动配置"。Spring Boot自动配置尝试根据你添加的jar依赖项自动配置你的Spring应用程序。供参考,你可以在这里查看所有的starters-https://www.baeldung.com/spring-boot-starters

Spring Boot还可以根据你使用的数据库自动配置dataSource bean。现在,不再使用spring-data-jpa,你应该添加spring-boot-starter-data-jpa。甚至不需要添加spring-boot-starter-jdbc,因为它已经包含在spring-boot-starter-data-jpa中。同样,你应该检查依赖项,比如Jakarta、jackson和jaxb是否已经由spring-boot-starter-web添加,那么你不需要明确添加它们。

如果你是Spring Boot的新手,可以从以下链接开始了解:

Spring Boot参考指南:-

Web- https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web
Data- https://docs.spring.io/spring-boot/docs/current/reference/html/data.html

英文:

First of all, you should have added more description to your question such as what you are trying to do along with some code snippet. Nevertheless, If you are creating spring-boot application, you should always use starter dependencies. Spring Boot starters can help to reduce the number of manually added dependencies just by adding one dependency and provides powerful feature i.e Auto-configuration. Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For your reference, you can see all the starters here- https://www.baeldung.com/spring-boot-starters

Spring Boot can also auto-configure the dataSource bean, depending on the database you're using. Now instead of using spring-data-jpa, you should add spring-boot-starter-data-jpa. Even you do NOT need to add spring-boot-starter-jdbc as it already comes with spring-boot-starter-data-jpa. In the same way, you should check if the dependencies like Jakarta, jackson, jaxb are already added by spring-boot-starter-web then you do not need to add those explicitly.

If you are new to Spring-Boot then for your reference start from here

Spring boot reference guide:-

Web- [https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web][1] 
Data- [https://docs.spring.io/spring-boot/docs/current/reference/html/data.html][1]

huangapple
  • 本文由 发表于 2023年3月31日 21:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定