-2

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);
  }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
TheKash
  • 21
  • 6
  • Is `getAllDepartments` working fine? – ray Sep 03 '21 at 17:43
  • Please post your complete error message including the stacktrace. I guess there is much more valuable information about the error available. – McPringle Sep 03 '21 at 17:49
  • @ray I just check getAllDepartment working fine, but I had to commented "//" all PostMapping from DepartmentController & AddDepartment from DepartmentService – TheKash Sep 03 '21 at 18:05
  • @McPringle This is all what I get com.learning.model.Employee cannot be converted to java.lang.Integer, and this error prevent initializing the app – TheKash Sep 03 '21 at 18:07
  • So you get the error at runtime? You should definitely see some more information where the error happens, like a class name or line number. – McPringle Sep 03 '21 at 18:12
  • What is this part? `emp.setName(department.getEmployees().get(emp.getId()))` This is wrong! So is this `Employee emp = employeeRepository.findById(department.getEmployees().get(department.getId())).orElse(null);` – ray Sep 03 '21 at 18:15
  • @ray the problem lies in the line as you identified, in fact I made before like this : findById(department.getEmployees().getId()) but getEmployees does not bring getId property then I did what Intellij propose. Please help if you have any solution $ – TheKash Sep 03 '21 at 18:26
  • What are you trying to do in `addDepartment` method? it seems overcomplicated and wrong! Are you just trying to save a `Department`? Why don't you simple call `departmentRepository.save(department)`? – pleft Sep 03 '21 at 18:54
  • I'm trying to save employee under department. As stated in begining one department can have many Employees – TheKash Sep 03 '21 at 21:50

1 Answers1

0

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

Seeschon
  • 408
  • 1
  • 9
  • 20