FetchType LAZY and EAGER Hibernate - Java @ Desk

Wednesday, March 5, 2014

FetchType LAZY and EAGER Hibernate

FetchType LAZY and EAGER Hibernate

In Hibernate, to define the relationships between two table we use following annotations :
1) @OneToOne
2) @ManyToOne
3) @ManyToMany
4) @OneToMany
from the javax.persistence package

When we load any entity from the database using the Hibernate session, there are two FetchType strategies we can use that belongs to javax.persistence.FetchType class :
1) LAZY - As the name suggests, relation entity that uses this FetchType will be lazily loaded i.e. it will be loaded from the database on demand. When we load the entity from the database it will not load the relationship entities. As an when required, the relationship entity can be called using the getter method.

2) EAGER - As the name suggests, relationship entity that uses this FetchType will be loaded as soon as the object entity is loaded from the database.

If you do not mention FetchType for relationship entity, hibernate will by default take that as FetchType LAZY.

Consider an example of an Insurance company which has set of employees in each geographical unit. We can create hibernate entity for this as shown below:

Insurance.java
package com.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "Insurance")
@NamedQueries({ @NamedQuery(name = "getInsuranceForLocation", query = "select t from Insurance t where t.location=?") })
public class Insurance {

    @Id
    @Column(name = "accountNumber")
    private String location;
    
    @OneToMany(fetch = FetchType.EAGER)
    private List<Employee> employees;

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}


Employee.java


package com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Employee")
public class Employee {

    @Id
    @Column(name = "employeeId")
    private String employeeId;

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
}


As shown above, as soon as we fire the getInsuranceForLocation Naming Query, Insurance object will be loaded from DB, it will also fetch all the employees working in that particular Geograplical Location since the relationship @OneToMany is using FetchType as EAGER.

If the FetchType is changed to LAZY, then fire the getInsuranceForLocation Naming Query, it will not load the Employee Collection object, but it will load the collection of Employees when the getter method "getEmployees()" is explicitly called.







No comments:

Post a Comment