'Hibernate - GET-request does not return the OneToOne related variable

I am sending a GET-request to my Spring Boot backend. This GET-request is suppossed to retrieve an employee-object from a database. This employee object is defined as follows:

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToOne(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = false)
    private EmployeeDetails employeeDetails;
}

Now, for some reason, the response that is returned does not contain the employeeDetails. If, on the other hand, I am sending a GET-request to retrieve the employeeDetails object, the response does contain the employee as well. The employeeDetails object looks like this:

@Entity
@Table(name = "employeet_details")
@Data
public class employeeDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "date_of_birth")
    private String birthDate;
    @Column(name = "address")
    private String address;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "employee_id")
    private Employee employee;
}

The GET-request asking for the employee is executing the following SQL to retrieve the employee information from the database:

  @Query("SELECT e FROM Employee e WHERE e.id = ?1")
  Optional<Employee> findEmployeeById(Long id);

Can someone explain this to me?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source