Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.5k points)

I am a newbie to Java Persistence API and Hibernate.

What is the difference between FetchType.LAZY and FetchType.EAGER in Java Persistence API?

1 Answer

0 votes
by (46k points)

Sometimes you have two items and there's a connection between them. For example, you might have an item called University and another item called Student.

The University item might have some fundamental characteristics such as id, name, address, etc. as well as a feature called students:

public class University {

 private String id;

 private String name;

 private String address;

 private List<Student> students;

 // setters and getters

}

Now when you set a University from the database, JPA loads its id, name, and address fields for you. But you hold two options for students: to load it concomitantly with the rest of the fields (i.e. eagerly) or to load it on-demand (i.e. lazily) while you call the university's getStudents() method.

While a university has many students it is not capable to load all of its students with it when they aren't required. So in suchlike circumstances, you can claim that you want students to be loaded if they are needed. This is called lazy loading.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 14, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 26, 2019 in Java by Ritik (3.5k points)

Browse Categories

...