Я работаю над проектом веб-приложения maven в Netbeans и у меня есть следующие классы:
@Entity
@Table(name = "departments")
public class Department{
@Id
private Integer departmentId;
@Column(name = "department_name")
private String departmentName;
}
И:
@Entity
@Table(name = "employees")
public class Employee {
@Id
private Integer employeetId;
@Column(name = "employee_name")
private String employeeName;
@JoinColumn(name = "department_id", referencedColumnName =
"department_id")
@ManyToOne(optional = false)
private Departments department;
}
Мои остальные API возвращают это:
{
employeeId:1,
employeeName:"Jhon",
department: { departmentId:1, departmentName:"IT"}
}
Мой желаемый вывод:
{
employeeId:1,
employeeName:"Jhon",
department: "IT"
}
Я пытаюсь вернуть DTO, но я получаю пустой JSON:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<EmployeeDto> findAllEmployees() {
CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(Employee.class);
cq.select(cq.from(Employee.class));
List<Employee> employees = entityManager.createQuery(cq).getResultList();
List<EmployeeDto> employeesDto = new ArrayList<>();
for (Employee employee : employees) {
EmployeeDto employeeDto = new EmployeeDto();
employeeDto.employeeId = employee.getEmployeedId();
employeeDto.department = employee.getDepartment().getDepartmentName();
employeesDto.add(employeeDto);
}
return employeesDto;
DTO:
Class EmployeeDto{Integer employeeId; String employeeName; String department}