1

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

  1. JPA Self Join using JoinTable

  2. Self join Spring JPA

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"
}

2 Answers2

1

You should use @JsonIgnore annotation in the fields you do not want to be included. Check here and here for more information.

  • That will not prevent anything from being fetched; also, how would that lead to having only one level of managers (instead of zero or all of them)? – OhleC Apr 05 '19 at 10:01
  • That is why I added the two extra links so he could search and find the appropriate configuration to achieve it. – Σωτήρης Ραφαήλ Apr 05 '19 at 10:02
  • Hi @ΣωτήρηςΡαφαήλ I did looked into the links you pointed out. ```@JsonIgnore``` completely blankets the line manager. Also ```@JsonManagedReference, @JsonBackReference``` doesn't solves my case. They will work on two different entities and not on the same entity joined with itself. – Aashirwad Gupta Apr 08 '19 at 11:16
  • @AashirwadGupta you could create a response object which you will fill it only with the information you want to return. – Σωτήρης Ραφαήλ Apr 08 '19 at 12:55
0

try using lazy load

@ManyToOne(fetch=FetchType.LAZY)
Ricardo
  • 111
  • 1
  • 9