You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot+OracleDB+JPA项目启动报UnsatisfiedDependencyException异常

Spring Boot JPA注入失败问题:Not a managed type

我正在开发一个Spring Boot POC,使用JPA Repository实现Oracle数据库的CRUD操作。控制器的API调用正常,但注入JPA仓库时应用启动失败,抛出以下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo': Error creating bean with name 'employeeRepo' defined in com.example.springjpaoracle.repo.EmployeeRepo defined in @EnableJpaRepositories declared on SpringjpaOracleApplication: Not a managed type: class com.example.springjpaoracle.pojo.Employee

项目结构

com.example.springjpaoracle
├── controller
│   └── EmployeeController.java
├── pojo
│   └── Employee.java
├── repo
│   └── EmployeeRepo.java
├── SpringjpaOracleApplication.java
└── resources
    └── application.properties

代码详情

主类代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringjpaOracleApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(SpringjpaOracleApplication.class, args);
    }
}

Employee实体类代码

package com.example.springjpaoracle.pojo;

import javax.persistence.*;

@Entity
@Table(name="employees")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column
    private String name;
    @Column
    private double salary;
    
    
    public Employee(String name, double salary) {
        super();
        this.name = name;
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
    }
    
}

EmployeeController代码

package com.example.springjpaoracle.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.springjpaoracle.pojo.Employee;
import com.example.springjpaoracle.repo.EmployeeRepo;

@RestController
public class EmployeeController {
    
    @Autowired
    EmployeeRepo employeeRepo;
    
    @PostMapping("/employee")
    public String postEmployee(@RequestBody Employee employee) {
        System.out.println(employee.getName());
        System.out.println(employee.getSalary());
        return "Employee Added Successfully "+employee.toString();
    }   
}

EmployeeRepo代码

package com.example.springjpaoracle.repo;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.springjpaoracle.pojo.Employee;

public interface EmployeeRepo extends JpaRepository<Employee, Long> {

}

application.properties配置

#DB Config
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=suryamokka
spring.datasource.password=surya1995
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver
spring.jpa.hibernate.ddl-auto=update

问题分析与修复方案

核心原因

Not a managed type异常表示Spring无法将Employee识别为JPA管理的实体类,结合代码排查出以下问题:

  1. 实体类缺少JPA要求的无参构造方法,导致无法实例化;
  2. 数据库驱动配置键名错误,缺少-name=后缀;
  3. Repo泛型ID类型与实体类ID类型不匹配;
  4. (可选)包扫描范围未显式指定,存在潜在识别风险。

具体修复步骤

1. 给Employee添加无参构造方法

修改Employee.java,补充无参构造:

public Employee() {
    super();
}

2. 修正application.properties驱动配置

将错误的配置行替换为:

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

3. 修正EmployeeRepo泛型类型

将JpaRepository<Employee, Long>改为与实体ID类型匹配的JpaRepository<Employee, Integer>:

public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
}

4. (可选)显式指定扫描范围

在主类添加注解明确实体和Repo的包路径,增强稳定性:

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
@EntityScan(basePackages = "com.example.springjpaoracle.pojo")
@EnableJpaRepositories(basePackages = "com.example.springjpaoracle.repo")
public class SpringjpaOracleApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(SpringjpaOracleApplication.class, args);
    }
}

完成以上修复后重启应用,即可解决JPA仓库注入失败的问题。

内容的提问来源于stack exchange,提问作者Surya Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 13:07:04