Java Spring boot: : 不是托管类型错误(对于 spring 来说非常新)

问题描述 投票:0回答:1

我对 Spring 非常陌生,仍在学习核心概念,所以如果我得到详细的答案,我将非常感激和感激。基本上,我正在创建一个 Web 应用程序,其服务有点像存储在嵌入式数据库中的员工的 REST 目录。 我正在尝试实现为员工提供服务的端点作为类的 JSON 表示形式。 首先,我尝试实现列出所有员工的端点

/employees
。 我收到错误:
Not a managed type: class springemployeecatalog.domain.Employee
请注意:我要求绝对不要在域类的开头使用任何注释(例如@entity) 据我了解,该应用程序中的流程应如下:

  1. 来自客户端的请求。
  2. 控制器处理请求并从服务层检索数据。
  3. 服务层使用存储库从数据库中获取数据。数据作为域对象检索。
  4. 服务层然后将领域对象转换为DTO。这是使用映射器完成的。
  5. DTO 被发送回控制器。
  6. 控制器将 DTO 作为响应发送到客户端。

以上几点如有错误,请指正。这是我写的代码:

Employee.java:

public class Employee {
    private final Long id;
    private final FullName fullName;
    private final Position position;
    private final LocalDate hired;
    private final BigDecimal salary;
    private final Employee manager;
    private final Department department;

    @JsonCreator
    public Employee(@JsonProperty("id") final Long id,
                    @JsonProperty("fullName") final FullName fullName,
                    @JsonProperty("position") final Position position,
                    @JsonProperty("hired") final LocalDate hired,
                    @JsonProperty("salary") final BigDecimal salary,
                    @JsonProperty("manager") final Employee manager,
                    @JsonProperty("department") final Department department) {
        this.id = id;
        this.fullName = fullName;
        this.position = position;
        this.hired = hired;
        this.salary = salary.setScale(5, RoundingMode.HALF_UP);
        this.manager = manager;
        this.department = department;
    }
//getters

部门.java:

public class Department {

    private final Long id;
    private final String name;
    private final String location;

    @JsonCreator
    public Department(@JsonProperty("id") final Long id,
                    @JsonProperty("name") final String name,
                    @JsonProperty("location") final String location) {
        this.id = id;
        this.name = name;
        this.location = location;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getLocation() {
        return location;
    }
//getters

EmployeeDTO.java:

public class EmployeeDTO {
    private Long id;
    private FullName fullName;
    private Position position;
    private LocalDate hireDate;
    private BigDecimal salary;
    private EmployeeDTO manager;
    private DepartmentDTO department;
    public Long getId() {
        return id;
    }

    public FullName getFullName() {
        return fullName;
    }

    public Position getPosition() {
        return position;
    }

    public LocalDate getHired() {
        return hireDate;
    }

    public BigDecimal getSalary() {
        return salary;
    }

    public EmployeeDTO getManager() {
        return manager;
    }

    public DepartmentDTO getDepartment() {
        return department;
    }
    public EmployeeDTO(Long id, FullName fullName, Position position, LocalDate hireDate, BigDecimal salary, EmployeeDTO manager, DepartmentDTO department) {
        this.id = id;
        this.fullName = fullName;
        this.position = position;
        this.hireDate = hireDate;
        this.salary = salary;
        this.manager = manager;
        this.department = department;
    }

}

departmentdto.java:

public class DepartmentDTO {
    private Long id;
    private String name;
    private String location;

    public DepartmentDTO(Long id, String name, String location) {
        this.id = id;
        this.name = name;
        this.location = location;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getLocation() {
        return location;
    }
}

employeerepository.java:

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

部门存储库.java:

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
}

employeeservice.java:

@Service
public class EmployeeService {
    private final EmployeeRepository employeeRepository;
    private final DepartmentRepository departmentRepository;

    @Autowired
    public EmployeeService(EmployeeRepository employeeRepository, DepartmentRepository departmentRepository) {
        this.employeeRepository = employeeRepository;
        this.departmentRepository = departmentRepository;
    }

    public List<EmployeeDTO> getAllEmployees() {
        return employeeRepository.findAll().stream()
                .map(this::convertToEmployeeDto)
                .collect(Collectors.toList());
    }

    public List<DepartmentDTO> getAllDepartments() {
        return departmentRepository.findAll().stream()
                .map(this::convertToDepartmentDto)
                .collect(Collectors.toList());
    }
    private EmployeeDTO convertToEmployeeDto(Employee employee) {
        Department department = employee.getDepartment();
        Employee manager = employee.getManager();
    
        return new EmployeeDTO(
                employee.getId(),
                employee.getFullName(),
                employee.getPosition(),
                employee.getHired(),
                employee.getSalary(),
                manager != null ? convertToEmployeeDto(manager) : null,
                department != null ? convertToDepartmentDto(department) : null
        );
    }

    private DepartmentDTO convertToDepartmentDto(Department department) {
        return new DepartmentDTO(
                department.getId(),
                department.getName(),
                department.getLocation()
        );
    }
}

emloyeecontroller.java:

@RestController
public class EmployeeController {
    private final EmployeeService employeeDepartmentService;

    @Autowired
    public EmployeeController(EmployeeService employeeDepartmentService) {
        this.employeeDepartmentService = employeeDepartmentService;
    }

    @GetMapping("/employees")
    public List<EmployeeDTO> getAllEmployees() {
        return employeeDepartmentService.getAllEmployees();
    }
}

数据库架构:

CREATE TABLE DEPARTMENT
(
    ID       INTEGER PRIMARY KEY,
    NAME     VARCHAR(14),
    LOCATION VARCHAR(13)
);

CREATE TABLE EMPLOYEE
(
    ID         INTEGER PRIMARY KEY,
    FIRSTNAME  VARCHAR(10),
    LASTNAME   VARCHAR(10),
    MIDDLENAME VARCHAR(10),
    POSITION   VARCHAR(9),
    MANAGER    INTEGER,
    HIREDATE   DATE,
    SALARY     DOUBLE,
    DEPARTMENT INTEGER,
    CONSTRAINT FK_DEPTNO FOREIGN KEY (DEPARTMENT) REFERENCES DEPARTMENT (ID),
    CONSTRAINT FK_EMPNO FOREIGN KEY (MANAGER) REFERENCES EMPLOYEE (ID)
);

COMMIT;

请告诉我是否需要提供额外的代码。 我知道我的代码可能会让您感到沮丧。提前抱歉。我正在尽力熟悉这个框架,因为到目前为止我从未接触过它。

java spring spring-boot spring-mvc spring-data-jpa
1个回答
0
投票

在域类上不使用 spring 注解是正确的直觉和良好的实践。您应该在您的域中继续使用

Employee
,但是一旦您达到
EmployeeRepository
,它就需要您通过
@Entity
带注释的类。因此,首选方法是将您的
Employee
域对象映射到
Entity
,例如称为
EmployeeEntity
并在您的
EmployeeRepository
中使用此类型。

@Repository
public interface EmployeeRepository extends JpaRepository<EmployeeEntity, Long> {
}

既然您在

Department
中也指的是
Employee
,那么
EmployeeEntity
应该指的是
DepartmentEntity
。具有值对象类型的其余字段(例如
FullName
)应该映射到 Spring JPA 能够理解的内容。在这种情况下,它将是
String

这样,您就可以查看 Spring boot - 不是托管类型,其中其他用户遇到了类似的错误,可以通过添加更多必需的注释来解决。

祝学习Spring一切顺利。

© www.soinside.com 2019 - 2024. All rights reserved.