我正在尝试学习SpringBootApplication并创建一个简单的应用程序应用程序,该应用程序使用存储库与'User'实体执行CRUD操作。由于某些原因,我一直有这个错误
非托管类型:class com.example.userApp.entities.User
我不知道为什么
这是主要功能:
package com.example.userApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
public class UserAppApplication {
public static void main(String[] args) {
SpringApplication.run(UserAppApplication.class, args);
}
}
这是存储库函数:
package com.example.userApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
public class UserAppApplication {
public static void main(String[] args) {
SpringApplication.run(UserAppApplication.class, args);
}
}
,这里是实体:包com.example.userApp.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String email;
}
我看到一些线程建议使用entitscan, enablejparepository和componentScan;我认为他们没有解决这个问题
Spring Boot 3。x(更具体地说是Spring Framework 6.x)从使用Java EE改为使用Jakarta EE (https://spring.io/blog/2021/09/02/a-java-17-and-jakarta-ee-9-baseline-for-spring-framework-6),因此您的javax.persistence.*
导入不再工作。您需要将它们替换为jakarta.persistence.*