views:

535

answers:

4

I am using Hibernate in NetBeans. I am using the hibernate util class that comes with the Hibernate plugin for NetBeans to get the current session. That is, I am using the following code to get my current session:

this.session = HibernateUtil.getSessionFactory().getCurrentSession();

But when I try to lazy fetch anything it gives following error:

org.hibernate.LazyInitializationException: failed to lazily initialize a course. 

I am using 2 DAO. One is Abstract DAO and second is CoutseDAO which extends AbstractDAO. code is as follows

public class AbstractDAO<T> {


    Session session = null;


    public AbstractDAO()
    {
      this.session = HibernateUtil.getSessionFactory().getCurrentSession();

   }

    public void createObject(T object)
    {
         Transaction tx = null;
        try
        {

            tx = session.beginTransaction();
            session.save(object);
            tx.commit();

        }
        catch (HibernateException e)
        {
            tx.rollback();
            throw new DataAccessLayerException(e);

        }
        finally
        {


        }
    }
    public  void updateObject(T object )
    {
         Transaction tx = null;
        try
        {

            tx = session.beginTransaction();
            session.update(object);
            tx.commit();

        }
        catch (HibernateException e)
        {
            tx.rollback();
            throw new DataAccessLayerException(e);

        }
        finally
        {


        }
    }

      public  void deleteObject(T object )
    {
         Transaction tx = null;
        try
        {

            tx = session.beginTransaction();
            session.delete(object);
            tx.commit();

        }
        catch (HibernateException e)
        {
            tx.rollback();
            throw new DataAccessLayerException(e);

        }
        finally
        {


        }
    }

}

second classs is as follows

public class CourseDAO extends AbstractDAO<Course>{

    public CourseDAO()
    {
        super();
    }

    public Course findByID(int cid){


        Course crc = null;
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query q = session.createQuery("from Course  as course where course.cid = "+cid+" ");
            crc = (Course) q.uniqueResult();
            tx.commit();
        } 
        catch (HibernateException e)
        {
            e.printStackTrace();
            tx.rollback();
            throw new DataAccessLayerException(e);
        }

        finally
        {


        }
        return crc;
}


    public List<Course> findAll(){

        List lst = null;
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query q = session.createQuery("from Course  ");
            lst =  q.list();
            tx.commit();
        }
        catch (HibernateException e)
        {
            e.printStackTrace();
            tx.rollback();
            throw new DataAccessLayerException(e);
        }

        finally
        {



        }
        return (List<Course>)lst ;
}







}
A: 

i am using 2 classe as follows 1)abstarctdao public class AbstractDAO {

Session session = null;


public AbstractDAO()
{
this.session = HibernateUtil.getSessionFactory().getCurrentSession();

}

public void createObject(T object)
{
     Transaction tx = null;
    try
    {

        tx = session.beginTransaction();
        session.save(object);
        tx.commit();

    }
    catch (HibernateException e)
    {
        tx.rollback();
        throw new DataAccessLayerException(e);

    }
    finally
    {


    }
}
public  void updateObject(T object )
{
     Transaction tx = null;
    try
    {

        tx = session.beginTransaction();
        session.update(object);
        tx.commit();

    }
    catch (HibernateException e)
    {
        tx.rollback();
        throw new DataAccessLayerException(e);

    }
    finally
    {


    }
}

  public  void deleteObject(T object )
{
     Transaction tx = null;
    try
    {

        tx = session.beginTransaction();
        session.delete(object);
        tx.commit();

    }
    catch (HibernateException e)
    {
        tx.rollback();
        throw new DataAccessLayerException(e);

    }
    finally
    {


    }
}

}

and second class is courseDAO

public class CourseDAO extends AbstractDAO{

public CourseDAO()
{
    super();
}

public Course findByID(int cid){


    Course crc = null;
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        Query q = session.createQuery("from Course  as course where course.cid = "+cid+" ");
        crc = (Course) q.uniqueResult();
        tx.commit();
    } 
    catch (HibernateException e)
    {
        e.printStackTrace();
        tx.rollback();
        throw new DataAccessLayerException(e);
    }

    finally
    {


    }
    return crc;

}

public List<Course> findAll(){

    List lst = null;
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        Query q = session.createQuery("from Course  ");
        lst =  q.list();
        tx.commit();
    }
    catch (HibernateException e)
    {
        e.printStackTrace();
        tx.rollback();
        throw new DataAccessLayerException(e);
    }

    finally
    {



    }
    return (List<Course>)lst ;

}

}

Don't augment questions with answers, edit your question instead. Please delete this. While you are at it, show some gratitude to those who take the time to answer you and accept some reasonable percentage of the answers to your (many) questions.
Software Monkey
A: 

The lazy fields are only accessible within the boundaries of the transaction that fetched the object.

Maurice Perry
can u suggest me a DAO implementation for my proplem
A: 

You may be the victim of 'stale' objects. When using Seam, I have this issue for injected objects. Our solution has been to re-fetch the main object before using, like calling:

getEm().find( MyClass.class, id );

Basically, whenever you call a method in our action classes, you need to do this to make sure that the object isn't stale in your current session / transaction.

Also, an exception (even if caught!) can close the current session, causing LazyInitialization errors.

Matt Lewis
A: 

You need to set the transaction manager so that hibernate can do the queries for the lazy fetching in the same transaction.

The hibernate site seems to be down but google has the cache

To enable the thread-bound strategy in your Hibernate configuration:

set hibernate.transaction.factory_class to org.hibernate.transaction.JDBCTransactionFactory
set hibernate.current_session_context_class to thread

There are other options, like JPA, EJB or rolling your own.

Your code have to start a transaction "manually" if you are not running in an appserver or use something else that handles transactions for you in some other way.

try {
  factory.getCurrentSession().beginTransaction();

   // Do some work
  factory.getCurrentSession().load(...);
  factory.getCurrentSession().persist(...);

  factory.getCurrentSession().getTransaction().commit(); 
}
catch (RuntimeException e) {
    factory.getCurrentSession().getTransaction().rollback();
    throw e; 
}
KarlP
where can i set this options? In the hibernate.cfg.xm file. if yes how?
i tried setting this parameters but it doesnt work with the same code
Your code have to start a transaction before doing anything.Try to read the doc, as it is a little bit complicated. Try the google cache link.
KarlP

related questions