In the previous post, we discussed the differences between the
A
Employee.java,
In the above example, Employee and Address entity have the One to Many relationship. The default fetch type of One to Many relationship is LAZY.
Address.java,
update() and merge() methods in Hibernate. In this post, we will discuss the causes of LazyInitializationException in Hibernate and explore the different approaches to resolve it.A
LazyInitializationException occurs when the Hibernate Session has been closed and the entity becomes detached. If you then attempt to access a lazily loaded property or associated object of the detached entity, Hibernate throws the following exception:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
In other words, Hibernate does not support lazy initialization for detached entities. Accessing a lazily loaded association outside the context of an active Hibernate Session results in a LazyInitializationException because Hibernate can no longer retrieve the required data from the database.
The following example demonstrates this scenario:
Employee.java,
@Entity @Table (name="employee") public class Employee { @Id @Column(name="id") private int id; @Column(name="emp_name") private String name; @OneToMany(cascade=CascadeType.ALL, FetchType.LAZY) @JoinColumn(name="address_id") private Set<Address> address; // setters and getters }
In the above example, Employee and Address entity have the One to Many relationship. The default fetch type of One to Many relationship is LAZY.
Address.java,
@Entity @Table (name="address") public class Address { @Id @Column(name="id") private int id; @Column(name="address") private String address; // setters and getters }
The Hibernate class to get the details from entity as follows,
HibernateMain.java,
package com.adnblog; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.adnblog.Employee; public class HibernateMain { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Employee emp = session.get(Employee.class, 1);
session.close();
emp.getId(); // will throw LazyInitializationException
sessionFactory.close();
}
}
The above code throw the error:--
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Solution:--
Use
FetchType.EAGER for the address association (mapped by the address_id foreign key) in the Employee entity. With FetchType.EAGER, Hibernate loads the associated Address entity along with the Employee entity when session.get() is called. As a result, even after the Hibernate Session is closed, you can access the Address details without encountering a LazyInitializationException.Thank you for visiting the blog!
Related Post:--
1) Hibernate - JPA Annotations with explanation
2) What are different states of an entity bean in Hibernate?
3) Difference between Update and Merge methods in Hibernate with example
4) What is a Hibernate Caching ? Explain first level and second level cache in Hibernate
5) What is the difference between get() and load() methods in Hibernate?
6) Hibernate JPA Cascade Types
7) Difference between save() and persist() method in Hibernate
8) What is lazy loading in Hibernate? Explain with example
No comments:
Post a Comment