skip to Main Content

I have 2 entity classes. One employee should has one department. But one department can has many employees.

When I send to postman request insert employee, I want to trigger Department table.

@Table
@Entity(name = "employee")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Employee extends Auditable<String> implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "employee_name", nullable = false)
    private String employeeName;

    @Column(name = "employee_surname", nullable = false)
    private String employeeSurname;

    @Column(name = "employee_email",
            nullable = false,
            unique = true)
    private String employeeEmail;

    @Column(name = "employee_identity_number",
            nullable = false,
            unique = true,
            length = 11)
    private String employeeIdentityNumber;


    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;


}

Above code my Employee class.

@Table
@Entity(name = "department")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long departmentId;

    @Column(name = "department_name", unique = true)
    private String departmentName;

    @OneToMany(mappedBy = "department")
    private Set<Employee> employees;

}

Above code my Department class.

When I send postman request as JSON like following,

{
    "employeeName": "Josh",
    "employeeSurname": "Long",
    "employeeEmail": "[email protected]",
    "employeeIdentityNumber": "12345678912",
    "department": 1
}

I know, department property is wrong. I should define as department object. But i want to send just department id.

And when I send this request, I want to trigger employees field in department table.

2

Answers


  1. You could have a dto object like this

    public class EmployeeDto{
        private String employeeName;
        private String employeeSurname;
        private String employeeEmail;
        private String employeeIdentityNumber;
        private long departmentId;
    }
    

    A controller mthod like this:

    @PostMapping("employee")
    public Employee save(@RequestBody EmployeeDto employeeDto){
        Employee em = employeeDto.toEntity();
        //Assuming department with given id already exist
        em.setDepartment(Department.builder().departmentId(employeeDto.departmentId).build());
        return emRepository.save(employee);
    }
    

    to my knowlage, this should do the job.

    Login or Signup to reply.
  2. you can try this,

    {
       "employeeName": "Josh",
       "employeeSurname": "Long",
       "employeeEmail": "[email protected]",
       "employeeIdentityNumber": "12345678912",
       "department": {"departmentId": "5342432432"}
    }
    

    and for department,

    {
       "departmentName": "department", 
       "employees" : [{"id" : "79878"}, {"id": "654654"}, ...]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search