Un ejemplo para diferenciar entre EAGER y LAZY en JPA.


Difference between FetchType LAZY and EAGER

The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched ( fetch in one 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 needed 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…

Ver la entrada original 62 palabras más