Difference between FetchType LAZY and EAGER in Java Persistence API

0 votes

I am a newbie to Java Persistence API and Hibernate.

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

Dec 5, 2018 in Java by Sushmita
• 6,920 points
2,624 views

1 answer to this question.

0 votes

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.

The University entity might have some basic properties such as id, name, address, etc. as well as a property called 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 students: 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 university's getStudents() method.

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

answered Dec 5, 2018 by Daisy
• 8,140 points
nice answer !

Related Questions In Java

0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 26, 2018 in Java by developer_1
• 3,350 points
1,256 views
0 votes
2 answers

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,190 points
63,389 views
0 votes
1 answer

What's the difference between SoftReference and WeakReference in Java?

Weak references A weak reference, simply put, is a ...READ MORE

answered Jun 12, 2018 in Java by Rishabh
• 3,620 points
2,001 views
0 votes
2 answers

What is the difference between getAttribute() and getParameter() in java?

getParameter() returns http request parameters. Those passed from ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
22,622 views
+1 vote
12 answers

Hibernate hbm2ddl.auto possible values and their uses

hibernate.hbm2ddl.auto (e.g. none (default value), create-only, drop, create, create-drop, validate, and update) Setting to perform SchemaManagementTool actions automatically as ...READ MORE

answered Dec 7, 2018 in Java by Shuvodip
77,234 views
0 votes
1 answer

Hibernate show real SQL

If you want to see the SQL ...READ MORE

answered Jul 5, 2018 in Java by Sushmita
• 6,920 points
1,056 views
0 votes
1 answer

object references an unsaved transient instance - save the transient instance before flushing

You should add cascade="all" (if using xml) ...READ MORE

answered Sep 26, 2018 in Java by Parth
• 4,640 points
34,799 views
0 votes
2 answers

What's the difference between @JoinColumn and mappedBy when using a JPA @OneToMany association?

JPA mapping annotation can be classified as ...READ MORE

answered Oct 28, 2020 in Java by bjjj
• 140 points
21,391 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,140 points
798 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,619 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP