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