Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
282 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...