纠正在将Spring Boot应用程序从JDK 8迁移到JDK 17时遇到的错误。

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

Correcting the error encountered while migrating a Spring Boot application from JDK 8 to JDK 17

问题

当我将我的项目从Java 8迁移到Java 17时,我遇到了以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userPreferencesDAO': Error creating bean with name 'userPreferencesDAO' defined in com.thetisit.felayer.repository.UserPreferencesDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.thetisit.felayer.entity.UserPreferences
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:482)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597)
	at com.thetisit.SecuredUsersFELayer.main(SecuredUsersFELayer.java:19)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userPreferencesDAO' defined in com.thetisit.felayer.repository.UserPreferencesDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.thetisit.felayer.entity.UserPreferences
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
	... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.thetisit.felayer.entity.UserPreferences
	at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:192)
	at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:465)
	at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:97)
	at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:80)
	at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:69)
	... 29 common frames omitted

这是应用程序中的入口类:

@ConfigurationPropertiesScan
@SpringBootApplication
public class SecuredUsersFELayer {

	public static void main(String[] args) {

		SpringApplication.run(SecuredUsersFELayer.class, args);
	}
}

我的实体类:

@Entity
@Table(name = "USERS_PREFERENCES")
public class UserPreferences {

	@Id
	private String username;

	private String preferences;
	
	// getters and setters
	.
	.
	.
}

在pom.xml文件中,我有spring-boot-starter-data-jpa依赖:

我的项目的父级是spring-boot-starter-data,从版本2.4.1迁移到3.1.0。

英文:

When I ported my project from Java 8 to Java 17, I encountered the following error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userPreferencesDAO': Error creating bean with name 'userPreferencesDAO' defined in com.thetisit.felayer.repository.UserPreferencesDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.thetisit.felayer.entity.UserPreferences
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:133)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:482)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597)
	at com.thetisit.SecuredUsersFELayer.main(SecuredUsersFELayer.java:19)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userPreferencesDAO' defined in com.thetisit.felayer.repository.UserPreferencesDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.thetisit.felayer.entity.UserPreferences
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
	... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.thetisit.felayer.entity.UserPreferences
	at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:192)
	at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:465)
	at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:97)
	at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:80)
	at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:69)
	... 29 common frames omitted

Here is the entry class in the application:

@ConfigurationPropertiesScan
@SpringBootApplication
public class SecuredUsersFELayer {

	public static void main(String[] args) {

		SpringApplication.run(SecuredUsersFELayer.class, args);
	}
}

My entity class:

@Entity
@Table(name = "USERS_PREFERENCES")
public class UserPreferences {

	@Id
	private String username;

	private String preferences;
	
	// getters and setters
	.
	.
	.
}

In the pom.xml file, I have the spring-boot-starter-data-jpa dependency:

The parent of my project is spring-boot-starter-data migrating from version 2.4.1 to 3.1.0.

答案1

得分: 1

我不理解你的问题,但我猜问题出在依赖项上。确保库和依赖项正常。

当你将JDK从8更改为17时,你应该将Javax包更改为Jakarta包

我猜这可能是你的问题。

英文:

I don't understand your problem, completely but I guess the problem is in the dependencies. Make sure the libraries and dependencies are OK.

and when you change the JDK from 8 to 17, you should change the Javax packages to Jakarta packages.

I guess this is your problem.

答案2

得分: 0

我怀疑你的 @Entity 类不是来自正确的包。这就是为什么 JPA 无法识别 UserPreferences 类的原因。

请尝试:

import jakarta.persistence.*;

@Entity
@Table 
@Table(name = "USERS_PREFERENCES")
public class UserPreferences { ...
英文:

I suspect your @Entity class is not from the correct package. That is why the UserPreferences class is not recognized by JPA.

Please try:

import jakarta.persistence.*;


@Entity
@Table 
@Table(name = "USERS_PREFERENCES")
public class UserPreferences { ...

huangapple
  • 本文由 发表于 2023年5月24日 18:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322302.html
匿名

发表评论

匿名网友

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

确定