In Spring Boot project I want to make one to many relationship but I am confused about rest output

I created one project using spring boot, spring data JPA. In that project I use Employee and Department entity in that ManyToOne bi-direction relationship at the time I run the project it is an unexpected result Employee.java

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
  @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String firstname;
        private String lastname;
        private Long mobile;
        private String email;
        @ManyToOne
        private Department department;

    public Employee(Long id, String firstname, Department department) {
        this.id = id;
        this.firstname = firstname;
        this.department = department;
    }} 

Department.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
private String Departmentname;
private String Address;
@OneToMany(mappedBy = "department",fetch = FetchType.EAGER)
private List<Employee>employees= new ArrayList<>();

    public Department(String departmentname, String address) {
        Departmentname = departmentname;
        Address = address;
    }

    public void addEmployees(Employee employee)
    {
        this.employees.add(employee);
    }
    public void removeEmployees(Employee employee)
    {
        this.employees.remove(employee);
    }
}

EmployeeRepository.java(I)

@Repository
public interface EmployeeRepositroy extends JpaRepository<Employee, Long> {

    @Query("SELECT e FROM Employee e join e.department WHERE e.firstname =:n")
    public List<Employee> getName(@Param("n") String firstname);

    @Query("SELECT e FROM Employee e")
    public List<Employee> getData();
}

EmployeeController.java

@RestController
public class EmployeeController {
    @Autowired
EmployeeRepositroy employeeRepositroy;
@GetMapping("/record/{name}")
    public Object GetNameByRecords(@PathVariable("name") String firstname)
{
return employeeRepositroy.getName(firstname);
}

}

Error is here https://docs.google.com/document/d/1fD5eChDowY7F8tAE4ORM_V7ulSt6vHe23SKmkx22M18/edit?usp=sharing



Read more here: https://stackoverflow.com/questions/68475405/in-spring-boot-project-i-want-to-make-one-to-many-relationship-but-i-am-confused

Content Attribution

This content was originally published by santosh patil at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: