json 对象 returns 空值 spring 引导

json object returns null spring boot

І 正在开发一个带有 Spring Boot 的 REST API 应用程序。我在数据库中有2个table,第一个叫TblEmployees,第二个叫TblDepartments。从这 2 tables 我做了一个 class Employees_DTO 应该 return 数据在 JSON 格式。从 TblEmployees table 它将 return 数据和从 TblDepartments table 它将 return 为空。 dbID 是一个外键,我按照我的想法做了,但对我来说不起作用:

    public class TblDepartments {
private Integer dbID;
private String dep_name;

public class TblEmployees {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int emp_id;
private String empName;
private Boolean empActive;
private Integer dbID;

public class Employees_DTO {
private int emp_id;
private String empName;
private Boolean empActive;
private String dep_name;
   }

package com.example.test_task.exceptions;

import com.example.test_task.*;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.modelmapper.spi.MatchingStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
 import java.util.stream.Collectors;

 @Service
public class EmployessServise {
@Autowired
private EmplployeesRepository repo;

@Autowired
private ModelMapper modelMapper;

public EmployessServise() {
}

public List<Employees_DTO> getAll_Emp(){
    return repo.findAll()
            .stream()
            .map(this::convertEntityToDto)
            .collect(Collectors.toList());
}

private Employees_DTO convertEntityToDto(TblEmployees tblEmployees){
    modelMapper.getConfiguration()
            .setMatchingStrategy(MatchingStrategies.LOOSE);
    Employees_DTO employees_dto=new Employees_DTO();
    employees_dto=modelMapper.map(tblEmployees,Employees_DTO.class);
    return employees_dto;
}

private TblEmployees convertEntityToDto(Employees_DTO employees_dto){
    modelMapper.getConfiguration()
            .setMatchingStrategy(MatchingStrategies.LOOSE);
    TblEmployees tblEmployees=new TblEmployees();
    tblEmployees=modelMapper.map(employees_dto,TblEmployees.class);
    return tblEmployees;
}
  }

@ManyToOne可用:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class TblEmployees {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int emp_id;
    private String empName;
    private Boolean empActive;
    @ManyToOne
    @JoinColumn(name = "dbid")
    private TblDepartments department;

    // Getters and Setters

}

full sample code