Let’s say we have entities like this:
@Entity
@Table
public class Parent {
@Id
private Long id;
@OneToMany(mappedBy="parent", fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
private List<Child> child;
//getter setters
}
@Entity
@Table
public class Child {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Parent parent;
//getter setter
}
In the example above, when getting Parent
entity, hibernate will automatically load all child
entities eagerly using join. On the other hand, when you fetch Child
, Parent
entity won’t be selected unless you call it explicitly in your code child.getParent()
.
FetchType (Lazy/Eager) tells whether we want entity to be loaded eagerly or lazy, when there’s call in code.
FetchMode (Select/Join) tells whether we want our entity to be loaded with additional select or in one query with join or subselect.