Stuck with error: java.util.Set<> cannot be converted to java.lang.Integer

I’m a beginner in spring boot, try to make an app with class Department & Employee. I make the relation between these two classes, Department can have many Employee whereas Employee can only have one Department. Every time I ended with an error:

com.learning.model.Employee cannot be converted to java.lang.Integer

Also, I’ve found two ways to inserting data into DB via API. First through the service layer, 2nd directly through the controller. Thankful if you could advise as to what is the most authentic method among the two above.

Department.java

@Entity
@Table(name = "Department")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Department {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dept_seq")
  @SequenceGenerator(initialValue = 1, name = "dept_seq", sequenceName = "dept_sequence")
  @Column(name = "id")
  private Integer id;
  
  @Column(name = "deptName")
  private String deptName;
  
  @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
  @JsonIgnore
  private List<Employee> employees;
}

Employee.java

@Entity
@Table(name = "Employee_Dtls")
@ToString
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Employee {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
  @SequenceGenerator(initialValue = 1, name = "emp_seq", sequenceName = "employee_sequence")
  @Column(name = "id")
  private Integer id;
  
  @Column(name = "name")
  private String name;
  
  @ManyToOne(cascade=CascadeType.ALL)
  @JoinColumn(name = "dept_id")
  private Department department;
}

DepartmentService.java

@Service
public class DepartmentService {

    @Autowired
    private EmployeeRepository employeeRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    //Get Department
    public List<Department> getAllDepartments() {
        return departmentRepository.findAll();
    }

    //Add Department
    public Department addDepartment(Department department) {
        Employee emp = employeeRepository.findById(department.getEmployees().get(department.getId())).orElse(null);
        if (null == emp) {
           emp = new Employee();
        }
        emp.setName(department.getEmployees().get(emp.getId()));
        department.setEmployees(emp);
        return departmentRepository.save(department);
    }
}

DepartmentController.java

public class DepartmentController {

  @Autowired
  private DepartmentService departmentService;

  @GetMapping("/get-departments")
  public ResponseEntity<List<Department>> getAllDepartments() {
    List<Department> departments = departmentService.getAllDepartments();
    return new ResponseEntity<>(departments, HttpStatus.OK);
  }
  
  @PostMapping("/department")
  public ResponseEntity<Department> saveDepartment(@RequestBody Department department) {
    Department dept = departmentService.addDepartment(department);
    return new ResponseEntity<>(dept, HttpStatus.OK);
  }
}

Answer

I think you must go over these stacks, these will probably help you to understand how @onetomany annotation works in spring

One to Many Relationship in spring boot REST Api POSTing oneToMany in a REST call via Spring Boot API