-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Lazy load and DAO pattern..
PostPosted: Fri Aug 06, 2004 12:32 am 
Newbie

Joined: Fri Aug 06, 2004 12:23 am
Posts: 3
I've implemented Session in View pattern to enable lazy load in DAO pattern.
Then, some part, the lazy load is very ok, but at few other, i catch this error at the bottom of the display page :

.....
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
....
Caused by: java.lang.NullPointerException
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3259)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:195)
... 45 more

So, can somebody here gives me an advise of what kind of this problem is ??
Thanks for reading..

PS: i'm using the latest hibernate version.

The implement of sessionfactory, i used the same as the follow

Code:
package de.gloegl.road2hibernate.util;

import java.io.*;
import javax.servlet.*;

import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.cfg.Configuration;

public class SessionManager implements Filter
{
    protected static ThreadLocal hibernateHolder = new ThreadLocal();
    protected static ThreadLocal txHolder = new ThreadLocal();
    protected static SessionFactory factory;
   
     public void init(FilterConfig filterConfig) throws ServletException
     {
        // Initialize hibernate
        try
        {
            factory = new Configuration().configure().buildSessionFactory();
        }
        catch (HibernateException ex) { throw new ServletException(ex); }
     }
     
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                   throws IOException, ServletException
    {
        if (hibernateHolder.get() != null)
            throw new IllegalStateException(
                "A session is already associated with this thread!  "
                + "Someone must have called getSession() outside of the context "
                + "of a servlet request.");
           
        try
        {
            chain.doFilter(request, response);
        }
        finally
        {
            Session sess = (Session)hibernateHolder.get();
            Transaction tx = (Transaction)txHolder.get();
            if (sess != null)
            {
                hibernateHolder.set(null);
               
                try
                {
                    if (tx != null) {
                        tx.commit();
                    }
                    sess.close();
                }
                catch (HibernateException ex) { throw new ServletException(ex); }
            }
        }
    }
   
    public static Session getSession() throws HibernateException
    {
        Session sess = (Session)hibernateHolder.get();
        Transaction tx = (Transaction)txHolder.get();
               
        if (sess == null)
        {
            sess = factory.openSession();
            txHolder.set(sess.beginTransaction());
            hibernateHolder.set(sess);
        } else {
            if (tx == null) {
                throw new IllegalStateException("Transaction was allready rolled back");
            }
        }   
        return sess;
    }
   
    public static void rollback() throws HibernateException {
        Transaction tx = (Transaction)txHolder.get();
        if (tx == null) {
            throw new IllegalStateException("Transaction was allready rolled back");
        }
       
        tx.rollback();
        txHolder.set(null);
    }
           
   
    public void destroy() {
        try {
            factory.close();
        }
        catch (HibernateException ex) { throw new RuntimeException(ex); }
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.