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 ?
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.
@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.
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.
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.
EXTRA = .size() and .contains() won't initialize the whole collection
TRUE = initialize the whole collection on first access
FALSE = Eager-Loading
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With