LAZY Loading not working for @EmbeddedId field

I have EmbeddedId which has field which need to be loaded lazy. In service class on fetching TeacherStudent it also fetch student EAGERLY which should be LAZY fetch. what changes are needed to make student being fetched as lazy ?

TeacherStudent.class —> making student load as lazy


 class TeacherStudent
 {
  public static final String TEACHER = "TeacherStudentPk.teacher";
  public static final String STUDENT = "TeacherStudentPk.student";

 @EmbeddedId
 TeacherStudentPk teacherStudentPk = new TeacherStudentPk();

 @ToString.Exclude
 @Transient
 Teacher teacher;

 @ToString.Exclude
 @Transient
 Student student; 
 }

Getting Teacher Student data from teacher table

TeacherStudentpk.class


  
  @Embeddable
 class TeacherStudentPk{
  @ManyToOne(fetch = FetchType.LAZY , optional = false)
  @JoinColumn(name = "student_pk") 
  Student student;   **<-------------  student marked as lazy**

  Teacher teacher;

 }

TecherService.class

class TeacherService{

    void getTeacherStudent(Teacher teacher){
        Set<TeacherStudent> teacherStudent = 
  teacher.getTeacherStudent(); **<-----eager 
  fetching student which is of lazy type** 
    }  
 }  

How should I change in classes to make student as fetch lazy ?

That’s not possible yet, maybe in Hibernate 6 it will be. You will need a mapping like this instead:

@Entity
class TeacherStudent {

 @EmbeddedId
 TeacherStudentPk teacherStudentPk = new TeacherStudentPk();

 @ManyToOne(fetch = FetchType.LAZY , optional = false)
 @JoinColumn(name = "teacher_pk", insertable = false, updatable = false) 
 Teacher teacher;

 @ManyToOne(fetch = FetchType.LAZY , optional = false)
 @JoinColumn(name = "student_pk", insertable = false, updatable = false) 
 Student student; 
 }
  
@Embeddable
 class TeacherStudentPk {
  @Column(name = "student_pk")
  int studentPk;
  @Column(name = "teacher_pk")
  int teacherPk;

}