Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between lazy loading and eager loading in JPA?

Tags:

jpa

public class University {
    private String id;
    private String name;
    private String address;
    private List<Student> students;

    // setters and getters
}

In lazily loading when I load a University from the database, JPA loads its id, name, and address fields for me. Students will not load. When I call getStudents() method, JPA will then execute the query

select * from students where universitycode=id

Is my understanding of lazy loading correct?


1 Answers

Correct. If you use eager loading on the other hand, JPA will proactively load students for you and return fully-populated University object.

Whether single JOIN query will be used or two separate queries is up to the JPA provider (EclipseLink, Hibernate...)

like image 127
Tomasz Nurkiewicz Avatar answered Dec 11 '25 03:12

Tomasz Nurkiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!