Cause and solution of “org.hibernate.LazyInitializationException: could not initialize proxy – no Session” error

Cause and solution of “org.hibernate.LazyInitializationException: could not initialize proxy – no Session” error

In Hibernate, a common error faced by lots of developer is “org.hibernate.LazyInitializationException: could not initialize proxy – no Session“. In lots of forum the answer is no answer and most of forum have answer but there is no explanation for the beginners.

First, Lets reproduce this error in Hibernate.

Assume that Hibernate configuration (hibernate.cfg.xml) and  mapping file is already written.

Consider below snap of code and assume that function is written in class “DBManager”.

 public List<ChatMessage> getMessages() {
            List<ChatMessage> MessageList = new ArrayList<ChatMessage>();
            Configuration cf = new Configuration().configure();
            SessionFactory factory = cf.buildSessionFactory();
            Session session = null;
            try {
                  session = factory.openSession();
                  String SQL_QUERY = "from ChatMessage c";
                  Query query = session.createQuery(SQL_QUERY);
                  Iterator<ChatMessage> it = query.iterate();
                  while (it.hasNext()) {
                        ChatMessage c = it.next();
                        MessageList.add(c);
                         //If below line is commented then code throws an error
                        //System.out.println("In Manager - Msg - "+c.getMessage());
                  }

            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                  session.close();
            }

            return MessageList;
      }

In above code, i am trying to fetch the collection of class “ChatMessage”. In while loop, simply i am adding the object in ArrayList and at the end i am returning the List. Check that i have commented the SOP statement at the end of while loop.

Lets consider that other class is invoking this code. Code snap of other class is:

List<ChatMessage> msgList = dbManager.getMessages();
                  System.out.println(" Size : "+msgList.size());
                  for(ChatMessage chatMsg : msgList)
                  {
                        out.println(olMsg+"<span style='background:#"+colorCode+"'>"+chatMsg.getUserName()+"</span>"+chatMsg.getMessage()+"<br/><br/>");
                  }

Whenever this code executes, it will throw an error inside for loop at “chatMsg.getUserName()” because the lazy loading is true by default and as error also describes this. The best solution is to make “lazyLoad=false” in configuration file. The code will then run perfect, but why this error did come?

This error means that you’re trying to access a lazily-loaded property or collection, but the hibernate session is closed or not available . Lazy loading in Hibernate means that the object will not be populated (via a database query) until the property/collection is accessed in code. Hibernate accomplishes this by creating a dynamic proxy object that will hit the database only when you first use the object. In order for this to work, your object must be attached to an open Hibernate session throughout it’s lifecycle.

When we uncomment the SOP statement, program runs successfully, because it hits the object and therefore it initializes itself through hibernate session.

Related posts

  • Well the solutions works, but it has a problem, when you use a LAZy=”FALSE” then everytime you call that DAO, its going to fetch all the data requested, including data from other tables, the real solution is something called Open Session in View, that unfortunately i dont quite comprehend, but its basically have the session variable for all the time we want

    • Hi Jeeba,
      Thanks for good comment.
      You are right. soon i am going to publish that solution.

      • Frank

        soon you say? when is soon: this year, this century, ’til the end of the world?

  • One more example to help to understand:

    s = sessions.openSession();
    Transaction tx = s.beginTransaction();

    User u = (User) s.createQuery("from User u where u.name=:userName")
    .setString("userName", userName).uniqueResult();
    Map permissions = u.getPermissions();

    tx.commit();
    s.close();

    Integer accessLevel = (Integer) permissions.get("accounts"); // Error!

  • Arun

    My mob no: +917204651195

  • adrienbe
  • asdad

    asdsad

  • vsr

    nice explanation