What is the use of the Hibernate @LazyCollection annotation

Rahul Agrawal picture Rahul Agrawal · Oct 17, 2012 · Viewed 29.6k times · Source

I have 2 entities as Parent and Child as OneToMany relation as

@Entity
public class Parent {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private String name;

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@IndexColumn(name = "index", base = 1)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@LazyCollection(LazyCollectionOption.EXTRA)
private List<Child> childs = new ArrayList<Child>();
// getter and setter

}

So here what is use of @LazyCollection(LazyCollectionOption.EXTRA) and when does it will come in picture, like for which operation with child list, it will be beneficial ?

Answer

wutzebaer picture wutzebaer · Sep 11, 2013

EXTRA = .size() and .contains() won't initialize the whole collection

TRUE = initialize the whole collection on first access

FALSE = Eager-Loading