Friday, December 27, 2013

org.hibernate.LazyInitializationException - could not initialize proxy - no Session



org.hibernate.LazyInitializationException - could not initialize proxy - no Session

Problem:
System.out.println("read action");
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
user=(User) session.load(pack.User.class, user.getId());
session.getTransaction().commit();
System.out.println(user);

Solution:
System.out.println("read action");
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
user=(User) session.load(pack.User.class, user.getId());

System.out.println(user);
session.getTransaction().commit();

Use the entity before commit it. Here, hibernate is using lazy initializing strategy to load the data from database and I was committing the session without fetching data.


0 comments: