I have an entity named employee which has self join with itself. The join is to represent who is the manager for that employee. When I query the records I am getting the whole hierarchy for the employee.
What I wish to achieve is to fetch only few fields of manager and do not wish to fetch an employee's manager's manager.
I tried doing it using @NamedEntityGraph
but not able to get the expected output.
Used Projections
as well.
I also tried the approach as mentioned here
Note - I do not wish to use @Query
annotation
@Entity
public class Employee {
@Id
@Column(name = "employeeId", nullable = false)
private String employeeId;
@Column(name = "firstName", nullable = false)
private String firstName;
@Column(name = "lastName", nullable = false)
private String lastName;
@Column(name = "middleName")
private String middleName;
@Column(name = "dateOfJoining", nullable = false)
private LocalDate dateOfJoining;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "line_manager_id")
private Employee manager;
}
Actual Result -
{
"firstName":"Super",
"lastName":"Manager",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Super",
"lastName":"Manager",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Admin",
"lastName":"User",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Admin",
"lastName":"User",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager": null,
"employeeId":"P67"
},
"employeeId":"P68"
},
"employeeId":"P69"
},
"employeeId":"P70"
}
Expected Result
{
"firstName":"Super",
"lastName":"Manager",
"middleName":"",
"dateOfJoining":"2012-12-31",
"manager":{
"firstName":"Super",
"lastName":"Manager",
"employeeId":"P69"
},
"employeeId":"P70"
}