-->
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.  [ 3 posts ] 
Author Message
 Post subject: Accessing data from a session closed somewhere else
PostPosted: Fri Apr 21, 2006 10:45 am 
Newbie

Joined: Fri Apr 21, 2006 10:35 am
Posts: 13
Location: de
Hi, I'm new here. I googled quite a lot to find an answer to my problem, but haven't found one yet. I'm not sure it hasn't been asked (and answered) in one way or another, though.

I'm using an EJB to find customers in my data base according to certain criteria. The EJB method looks roughly like this:
Code:
   public List findCustomers(parameters for the different criteria) {
      Session session = HibernateUtils.currentSession();
      Transaction tx = null;
      List foundCustomers = null;
      try {
         tx = session.beginTransaction();
         Query query = session.createQuery(some HQL query);
         foundCustomers = query.list();
         tx.commit();
      }
      catch (HibernateException e)
      {
         if (tx != null) {
            try {
               tx.rollback();
            }
            catch (HibernateException e1)
            {
            }
         }
         throw e;
      }
      finally {
         session.close();
      }
      return foundCustomers;
   }

Now if I want to work with the found customers in some other class, Hibernate complains that the session is closed.

I read something about initializing, so I tried to apply it to my case, inserting the following code before the commit:
Code:
         for (Iterator it = foundCustomers.iterator(); it.hasNext(); ) {
            Customer theCustomer = (Customer) it.next();
            Hibernate.initialize(theCustomer);
            if (theCustomer.getAddresses() != null)
               Hibernate.initialize(theCustomer.getAddresses());
         }

In my model, every customer has a (possibly empty, possibly null) set addresses. Is there a way to avoid the second initialization? Maybe the

other class does not only want to use the customer's address, but other associated data as well.

Unfortunately the code addition above gives me a null pointer exception in the second initialization. theCustomer.getAddresses() can't be

null at this point of the code, so what's wrong?

I don't want to manipulate the found customer in the other class, I just want to read its data and some associated data (such as the addresses).

By the way: I'm working with Hibernate 3, Oracle 10.2.1, and JBoss 4.0.1 SP1.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 1:41 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
for example:
suppose your query is
Code:
from
   Customers c
where
   c.name like :name

then you would need
Code:
from
   Customers c
   left outer join fetch c.addresses
where
   c.name like :name

to get only addresses early
or
Code:
from
   Customers c fetch all properties
where
   c.name like :name

to get every property early


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 6:29 am 
Newbie

Joined: Fri Apr 21, 2006 10:35 am
Posts: 13
Location: de
Thanks for the hint, pepelnm. I changed my HQL query accordingly, but strangely enough I got a NullPointerException in the last line of the following code:

Code:
String queryString =
     "select crn.customer " +
     "from CustomerRefNumber crn" +
     " left outer join fetch crn.customer.addresses" +
     " where value = '" + businessPartnerNumber + "'";
Query query = session.createQuery(queryString);
List foundCustomers = query.list();

The query variable is definitely not null (I checked it).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.