Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between FetchType LAZY and EAGER in Java Persistence API?

I am a newbie to Java Persistence API and Hibernate.

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

like image 405
leon Avatar asked Jun 07 '10 15:06

leon


People also ask

What is the difference between lazy and eager?

Lazy Loading vs. Eager Loading. While lazy loading delays the initialization of a resource, eager loading initializes or loads a resource as soon as the code is executed. Eager loading also involves pre-loading related entities referenced by a resource.

What is the difference between eager and lazy fetching Hibernate?

Eager Loading is a design pattern in which data initialization occurs on the spot. Lazy Loading is a design pattern that we use to defer initialization of an object as long as it's possible.

What is FetchType eager in JPA?

EAGER fetching tells Hibernate to get the related entities with the initial query. This can be very efficient because all entities are fetched with only one query. But in most cases it just creates a huge overhead because you select entities you don't need in your use case. You can prevent this with FetchType. LAZY.

Is FetchType lazy default?

Okay, so we talked about the fact that FetchType. LAZY is the default fetch type for all Hibernate annotation relationships.


1 Answers

Sometimes you have two entities and there's a relationship between them. For example, you might have an entity called University and another entity called Student and a University might have many Students:

The University entity might have some basic properties such as id, name, address, etc. as well as a collection property called students that returns the list of students for a given university:

A university has many students

public class University {    private String id;    private String name;    private String address;    private List<Student> students;     // setters and getters } 

Now when you load a University from the database, JPA loads its id, name, and address fields for you. But you have two options for how students should be loaded:

  1. To load it together with the rest of the fields (i.e. eagerly), or
  2. To load it on-demand (i.e. lazily) when you call the university's getStudents() method.

When a university has many students it is not efficient to load all of its students together with it, especially when they are not needed and in suchlike cases you can declare that you want students to be loaded when they are actually needed. This is called lazy loading.

Here's an example, where students is explicitly marked to be loaded eagerly:

@Entity public class University {      @Id     private String id;      private String name;      private String address;      @OneToMany(fetch = FetchType.EAGER)     private List<Student> students;      // etc.     } 

And here's an example where students is explicitly marked to be loaded lazily:

@Entity public class University {      @Id     private String id;      private String name;      private String address;      @OneToMany(fetch = FetchType.LAZY)     private List<Student> students;      // etc. } 
like image 81
Behrang Avatar answered Sep 19 '22 14:09

Behrang