-->
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: (newbee) How to NOT fetch a collection
PostPosted: Thu Nov 26, 2009 4:13 pm 
Newbie

Joined: Thu Nov 26, 2009 4:09 pm
Posts: 1
Hi everybody. I'm having a problem with Hibernate, I've googled a lot with no success.

I have a class "Lots" which is part of a 1-to-many relationshio with a class "Documents", I mean one lot has N documents (a collection of documents). The problem is, sometimes I don't want hibernate fetch the documents, I just want query the Lots data. But I don't know how to do that.

Below follows my mapping:

Code:
  <class name="Lots" table="LOTS">
   
     <id name="loteID" type="java.lang.Integer" column="LOT_ID">
        <generator class="native" />
     </id>

     <property name="dateLote" type="java.util.Date" column="DATE_LOTE" not-null="false"/>
     <property name="qtyDocuments" type="java.lang.Integer" column="QTY_DOCUMENTS" not-null="false"/>

   <!-- Documents Set -->
   <set name="lotDocs"
      inverse="true"
           cascade="all-delete-orphan"
           lazy="true">
      <key column="LOT_ID"/>
      <one-to-many class="com.xxx.sss.documents.Documents" />
   </set>
  </class>

This is the code to recover the data:
Code:
public Lots getById(int lotId) {

Session session = HibernateUtil.getSessionFactory().openSession();
Lots lot = null;
      
try {

      lot = (Lots) session.get(Lots.class, new Integer(lotId));
         
} catch (HibernateException ex) {
       throw new MyException("Error recovering Lot data", ex);
} finally {
       //session.close();       //  ---> PROBLEM
    }
    return lot;
}


since I'm ina webservice context, if I close the session I got a LazyException error, becouse Hibernate is still fetching data for the Documents collection (even with lazy="true")

My question is how to not fetch Documents data, in order I'm able to close the session without got a LazyException?

For the moments I need to get the Documents data froma certain Lot, I have another method (which is working fine):

Code:
public Set<Documents> listDocsByLotNumber(int lotId) {

      Session session = HibernateUtil.getSessionFactory().openSession();
      Set<Documents> myList = null;
      
      try {
         Lots l = (Lots) session.get(Lots.class, lotId);
         Hibernate.initialize( l.getLotDocs() );
         myList = l.getLotDocs();
      } catch (HibernateException ex) {
         throw new MyException("Can't get Documents list", ex);
      } finally {
         session.close();
      }
      return myList;
}


Could anybody help me? I'm very very begginer using Hibernate.

Thanks in advance


Top
 Profile  
 
 Post subject: Re: (newbee) How to NOT fetch a collection
PostPosted: Fri Nov 27, 2009 11:45 am 
Newbie

Joined: Mon Nov 28, 2005 2:35 pm
Posts: 14
Hi!

Show the stacktrace.


Top
 Profile  
 
 Post subject: Re: (newbee) How to NOT fetch a collection
PostPosted: Fri Nov 27, 2009 1:04 pm 
Newbie

Joined: Wed Oct 21, 2009 7:33 am
Posts: 11
Try using the lazy="extra". This will not initialize your collections, especially designed for large collections.


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.