# Difference between Lazy and Eager fetchType in the Java Persistence.

I have developed a small application i used the FetchType.LAZY and  FetchType.EAGER , I didnt find any difference.

Let me know the exact difference between FetchType.LAZY and FetchType.EAGER in Java persistence?

The EAGER fetch is a requirement on the persistence provider runtime that data must be eagerly fetched ( fetch in a single instance of query )

The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed( fetch when its required as sub-queries).

For Example :

you might have an entity called Student and another entity called Courses .

The Student entity might have some basic properties such as id, name etc. as well as a property called Courses .

Now when you load a Student from the database, JPA loads its id, name fields for you. But you have two options for Courses : to load it together with the rest of the fields (i.e. eagerly) or to load it on-demand (i.e. lazily) when you call the Student s getCourses() method-This is called  eager loading.

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

• EAGER Fetch  get results in one query ( parent and child )
• LAZY Fetch  get results as sub-query .