I have two entities:
Book
:
@Entity
public class Book {
@Id
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "library_id")
private Library library;
// Getters and setters
}
and Library
:
@Entity
public class Library {
@Id
private Long id;
@OneToMany(mappedBy = "library", fetch = FetchType.EAGER)
private List<Book> books;
// Getters and setters
}
What I want to do is eagerly fetch all the books for a given queried library:
Library library = em.find(Library.class, 1L);
System.out.println(library.getBooks());
But it gives me null
. How do I get list of all the linked books? I have searched and tried many solutions from S.O. but none works.
P.S. - I can assure you that there is linked data present in the tables.