I have 2 entities, Student and Note:
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "student")
private Note note;
}
@Entity
public class Note {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id")
private Student student;
}
If I try to fetch say 100 students using studentRepositoy::findAllById
, Hibernate hits a in
query and passes all the IDs. That’s good but then it’ll fire 100 queries more to get the note for each student. The FetchType.LAZY
doesn’t help here. I also tried FetchMode.JOIN
and FetchMode.SELECT
but they didn’t solve the problem either.
Is there a way we could fetch the note along with the student. I mean this is just a simple example, but in production the parent class has multiple entities like Note that are inversely joined and while fetching 1000s of entries it is very time-consuming.
Thanks in Advance.