Spring Data JPA操作Postgres报错:Not a managed type,求助排查
问题排查与修复方案
1. 核心原因:Spring Boot 3.0 与 Jakarta EE 包不兼容
Spring Boot 3.0 基于 Jakarta EE 9+,不再支持旧的 javax.persistence 包,你的RawData实体类导入的是旧包,导致JPA无法识别为托管实体。
修复:
将实体类的导入替换为Jakarta EE的包,并补充JPA要求的无参构造器与规范的字段访问方式:
package com.example.testcontroller.entity; import jakarta.persistence.*; // 替换原来的javax.persistence.* @Entity @Table(name="RawData") public class RawData { @Id @GeneratedValue(strategy= GenerationType.AUTO) private Long id; private double timestamp; private double energy_value; private String topics; // JPA要求的无参构造器 public RawData() {} // 生成getter和setter方法 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public double getTimestamp() { return timestamp; } public void setTimestamp(double timestamp) { this.timestamp = timestamp; } public double getEnergy_value() { return energy_value; } public void setEnergy_value(double energy_value) { this.energy_value = energy_value; } public String getTopics() { return topics; } public void setTopics(String topics) { this.topics = topics; } }
2. 配置类重复导入与冲突
你的配置类存在重复导入和手动配置与Spring Boot自动配置冲突的问题:
ApplicationConfig导入了InfrastructureConfig和PlainJpaConfig,而PlainJpaConfig又导入了InfrastructureConfig,导致重复加载配置。- 手动配置
LocalContainerEntityManagerFactoryBean、DataSource等,会覆盖Spring Boot的自动配置,且包扫描逻辑易出问题。
修复:
简化配置,删除冗余配置类,利用Spring Boot自动配置:
- 删除
PlainJpaConfig和InfrastructureConfig,将JPA配置合并到启动类或简化后的ApplicationConfig:
package com.example.testcontroller; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.boot.autoconfigure.domain.EntityScan; @Configuration @EnableJpaRepositories(basePackages = "com.example.testcontroller.repository") @EntityScan(basePackages = "com.example.testcontroller.entity") public class ApplicationConfig { }
- 在
application.properties中配置数据库连接,替代手动DataSource配置:
spring.datasource.url=jdbc:postgresql://localhost:5460/greensoftdb spring.datasource.username=user spring.datasource.password=pwd spring.datasource.driver-class-name=org.postgresql.Driver # JPA/Hibernate配置 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
3. 依赖冗余与版本冲突
你的pom.xml存在不必要的依赖和版本不匹配问题:
mysql-connector-java:使用PostgreSQL数据库,无需该依赖。hibernate-entitymanager:Spring Boot 3.0的spring-boot-starter-data-jpa已包含Hibernate 6.x依赖,手动添加旧版本会导致冲突。h2:如果不需要H2数据库,可直接移除。
修复:
修改pom.xml,保留必要依赖:
<?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.0.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>testcontroller</artifactId> <version>0.0.1-SNAPSHOT</version> <name>testcontroller</name> <description>testcontroller</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.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20220924</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> </project>
4. 启动类优化(可选)
@SpringBootApplication已包含@ComponentScan,可直接在启动类上添加JPA相关注解,删除单独的ApplicationConfig:
package com.example.testcontroller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.boot.autoconfigure.domain.EntityScan; @SpringBootApplication @EnableJpaRepositories(basePackages = "com.example.testcontroller.repository") @EntityScan(basePackages = "com.example.testcontroller.entity") public class TestcontrollerApplication { public static void main(String[] args) { SpringApplication.run(TestcontrollerApplication.class, args); } }
内容的提问来源于stack exchange,提问作者Liko
相关产品推荐
相关产品推荐

