Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the Hibernate @LazyCollection annotation

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 ?

like image 776
Rahul Agrawal Avatar asked Oct 17 '12 06:10

Rahul Agrawal


People also ask

What is the use of annotation in hibernate?

Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. All the metadata is clubbed into the POJO java file along with the code, this helps the user to understand the table structure and POJO simultaneously during the development.

What is the use of @entity annotation in hibernate?

@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.

Is @entity annotation mandatory in hibernate?

The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an entity. So, if you use annotations for mappings, @Entity is mandated by the specification and Hibernate has to adhere to it.

What is use of lazy loading in hibernate?

Hibernate now can "lazy-load" the children, which means that it does not actually load all the children when loading the parent. Instead, it loads them when requested to do so. You can either request this explicitly or, and this is far more common, hibernate will load them automatically when you try to access a child.


2 Answers

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

TRUE = initialize the whole collection on first access

FALSE = Eager-Loading

like image 87
wutzebaer Avatar answered Sep 22 '22 11:09

wutzebaer


There's actually no reason to use @LazyCollection.

The TRUE and FALSE values are not needed since the same behavior can be obtained with the JPA FetchType.LAZY or FetchType.EAGER.

The EXTRA value has no equivalent in JPA and was designed for very large collections. When you access an EXTRA lazy collection for the first time, the collection is not entirely loaded, as it's usually the case with any JPA collection.

Instead, each element is fetched one by one, using a secondary SELECT. This might sound like an optimization, but it's not because EXTRA lazy collections are prone to N+1 query issues.

Note that this only works for ordered collections, either List(s) that are annotated with @OrderColumn or Map(s). For bags (e.g. regular List(s) of entities that do not preserve any certain ordering), the @LazyCollection(LazyCollectionOption.EXTRA) behaves just like any other LAZY collection (the collection is fetched entirely upon being accessed for the first time).

If you have a very large collection, then you should not map it at all. Instead, you should map only the @ManyToOne side, and, instead of a parent-side collection, you should use a paginated JPQL query.

JPQL queries are much easier to tune because you can apply any filtering criteria, and you can paginate the result set.

like image 32
Vlad Mihalcea Avatar answered Sep 23 '22 11:09

Vlad Mihalcea