-->
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.  [ 2 posts ] 
Author Message
 Post subject: Session problems
PostPosted: Fri May 28, 2010 6:26 am 
Newbie

Joined: Thu Apr 01, 2010 6:47 am
Posts: 7
Hello,

I have a problem with sessions.

Here is the code I'm trying to run in the Main method

Code:
        Session s = HibernateSessionFactory.getSession();
        PEnforcementOrder order = null;
        try {
            order = PEnforcementOrder.getById(53);
        } catch (DbObjectNotFoundException donfe) {
            donfe.printStackTrace();
        }
        for(Iterator<PEnforcementDesignation> it = order.getEnforcementDesignations().iterator(); it.hasNext();)
            System.out.println(it.next().getDesignation());
        s.close();



Here is the code of the getById() method (I'm aware of the session.load() function so this is just example)

Code:
    public static PEnforcementOrder getById(long id) throws DbObjectNotFoundException {
        Session s = HibernateSessionFactory.getSession();
        String query = "from PEnforcementOrder e where enforcementOrderId=:id";
        Query q = s.createQuery(query);
        q.setLong("id", id);
       
        List<PEnforcementOrder> l = q.list();
       
        s.close();
       
        if(l.isEmpty())
            throw new DbObjectNotFoundException(query + " " + id);
       
        return l.iterator().next();
       
    }


I'm getting the error:
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: si.nkbm.ris.persistance.PEnforcementOrder.enforcementDesignations, no session or session was closed

It's obvious that the session which is opened in the Main method closes in the getById method and because of that the EnforcementOrder object is not usable afterwards to connect to other persistant classes.

How to handle sessions to avoid such type of behaviour. I need those functions to retrieve objects and then to connect to other objects?

Thanks


Top
 Profile  
 
 Post subject: Re: Session problems
PostPosted: Fri May 28, 2010 7:44 am 
Newbie

Joined: Thu Apr 01, 2010 6:47 am
Posts: 7
I found this solution and am not sure if it's ok. The Main method stays the same.

Code of the getById is:
Code:
    public static PEnforcementOrder getById(long id) throws DbObjectNotFoundException {
        Session s = HibernateSessionFactory.getSessionFactory().getCurrentSession();
        Transaction tx = s.beginTransaction();
        String query = "from PEnforcementOrder e where enforcementOrderId=:id";
        Query q = s.createQuery(query);
        q.setLong("id", id);
       
        List<PEnforcementOrder> l = q.list();
       
        if(l.isEmpty())
            throw new DbObjectNotFoundException(query + " " + id);
       
        return l.iterator().next();
    }


Also had to put in hibernate.cfg file line:
<property name="current_session_context_class">thread</property>

But there is not commiting or ending transaction so I'm little bit suspicious.


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

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.