In previous post, we discussed the difference between the update and merge methods in Hibernate. In the current post we will discuss Cause and Solution of LazyInitializationException in Hibernate.
This exception occurs when the Hibernate Session is closed and object becomes detached. And if you try to access properties or associated object of Detached object then it results in org.hibernate.LazyInitializationException: could not initialize proxy - no session Exception.
In other words, we can say Hibernate does not support lazy initialization for detached objects. Access to a lazy association outside of the context of an open Hibernate session will result in an LazyInitializationException.
Here is the example:--
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,
This exception occurs when the Hibernate Session is closed and object becomes detached. And if you try to access properties or associated object of Detached object then it results in org.hibernate.LazyInitializationException: could not initialize proxy - no session Exception.
In other words, we can say Hibernate does not support lazy initialization for detached objects. Access to a lazy association outside of the context of an open Hibernate session will result in an LazyInitializationException.
Here is the example:--
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 of field address_id in Employee entity, So that whenever you call the session.get() method, will load all the details from database. Even if you close the session you will get employee details.
Thank you for visiting 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