data fetching

Two options for fetching data are possible: FetchType.EAGER and FetchType.LAZY. Collections are loaded EAGER when they are fetched fully at the time their parent is fetched. That is, with nested classes the whole class hierarchy is fetched, even if all the subentities (children) are not needed.

With the LAZY option, data is loaded on-demand, e.g. when a certain entity is requested explicitly. Thus, when the parent entity is loaded with the lazy option, the children entities are not loaded per default. Only in case they are really needed (e.g. a certain method was called via GUI).
Here is an example how entities are marked to be loaded lazily:

@Entity
public class Parent {

@Id
private String id;

private String name;

@OneToMany(fetch = FetchType.LAZY)
private List children;

// etc.
}

Technology:

Subscribe to data fetching