In my spring application , Lazy fetch is not working which is making fetch operation from db heavy. So, when I fetch students It is fetching fields EAGERLY which are even marked as Lazy. My classes are Student.class
class Student{
Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
@JoinColumn(name = "tutor_fk", nullable = false)
Tutor tutor;
@OneToMany(fetch = FetchType.LAZY, mappedBy = Courses.student, cascade = CascadeType.ALL)
Set<Courses> courses = new HashSet<>();
}
Courses.class
class Courses{
Long id;
String create_date_time;
@ManyToOne
@JoinColumn(name = "student_fk")
Student student;
}
Tutor.class
class Tutor{
Long id;
String name;
}
studentService.class
@Transactional
class studentService{
List<Students> getStudent(Long id){
return studentRepository.findOneById(id);
} }
when I run my code on org.hibernate.SQL: DEBUG. querys hit are
select course0_.id as id1_43_0_, course0_.create_date_time as create_date_time2_43_0_
from course course0_ where course0_.id=?
select tutor0_.id as id1_43_0_, tutor0_.name as name2_43_0_
from tutor tutor0_ where tutor0_.id=?
Where I am doing wrong that it is hitting above query as eager when these fields are marked LAZY
question from:https://stackoverflow.com/questions/66050574/lazy-fetch-not-working-in-onetotomany-mapping