• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to NOT fetch a collection?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


This is the code to recover the data:


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):



Could anybody help me? I'm very very begginer using Hibernate.
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valmir Cinquini..

You could try to use HQL for getting the an Object or list of Objects that you want..



Hope that help..

Please correct me if i'm wrong..
Thanks..
 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leonardo, thanks for your help.

My problem is exactly the opposite. Actually I can get all documents from a lot. I would know how to query a lot without hibernate query the documents which belongs to that lot.

My problem is on the code below




This code queries the database and get the data from Lots but it ALSO get all data from Documents that are related to the lots. I'd like to Hibernate perform a query that brings only Lot data, even when a relationship between Lots and Documents exist.

So I will be able to close the session (thi line with the 'problem' comment).

Thanks for your interest. (and excuse me my poor English)


Leonardo Carreira wrote:Hi Valmir Cinquini..

You could try to use HQL for getting the an Object or list of Objects that you want..



Hope that help..

Please correct me if i'm wrong..
Thanks..

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not clear to me why you'd get a lazy exception when closing the session--are you sure that's when it's happening?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate will either get the data or create Proxy objects for your data. In the case above, you are not getting the data in the Collection, so the Collection has Proxies in it.

When you are outside of your session and you try to access that collection in any way, including size(), the Proxy will try to load the data, but since there is no Session around anymore, which means no Connection to the database, then it will through a LazyInitializationException. Because you don't have the data and now you are trying to get it without a means to get it.

Fetching strategies to me are the most important aspect of Hibernate, to make your queries use case driven, and then fetch exactly what you need, no more , no less. Btw, there are more than just Eager and Lazy fetching strategies, and this is a common interview question I use that very very rarely anyone gets correct or knows.

Hope that helps.

Mark
 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:
When you are outside of your session and you try to access that collection in any way, including size(), the Proxy will try to load the data, but since there is no Session around anymore, which means no Connection to the database, then it will through a LazyInitializationException. Because you don't have the data and now you are trying to get it without a means to get it.



That's my problem. Hibernate is trying to load the collection even when I dont want to. I'm not calling size() or trying to read any item from the collection. But Hibernate loads the collection data. As the session is closed, so I got the exception. How to avoid that?

I'd like something like "Hey Hibernate, don't load the collection now, I dont need t right now" and sometimes "Hey Hibernate, please load the data and the collection data too".

The only workaround I've thought was working with two mappings. One of then has the relationship. The other one doesn't have. So I will work with two DAO classes, one of them with methods to get the collection. I'm pretty sure this is not the correct aproach.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Valmir Cinquini wrote:

That's my problem. Hibernate is trying to load the collection even when I dont want to. I'm not calling size() or trying to read any item from the collection. But Hibernate loads the collection data. As the session is closed, so I got the exception. How to avoid that?



I would have to say that you are accessing it somewhere in your code/jsp after the session is closed. Accessing the collection is the only way Hibernate will go and try to load the Collection when it is proxies. It will not load the Collection unless you touch it. So somewhere over the rainbow, somewhere high, there is touching going on.

Hope that helps.

Mark
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope the following article might help you

Hibernate: Understanding Lazy Fetching
 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's my code. Do you suspect which point it says Hibernate should load the collection too?

public List<Lote> list() throws MDIException {

Session session = HibernateUtil.getSessionFactory().openSession();
List<Lote> lista = null;
Transaction tr = null;

try {

tr = session.beginTransaction();

lista = (List<Lote>) session.createQuery("from Lotes").list();

tr.commit();

} catch (HibernateException ex) {
tr.rollback();
throw new MDIException("Can't get list of Lotes", ex);

} finally {
session.close();

}
return lista;
}

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely something uses the list that's returned--why else would you fetch the list?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code or jsp/jstl tags is after a call to that method, not that method. If you can step through the code and find it loading the collection before the commit/close call then let us know, but it won't fetch it in that call, unless you have it set as eager fetch with I think you said it didn't

Mark
 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.



Excuse me.
 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:The code or jsp/jstl tags is after a call to that method, not that method. If you can step through the code and find it loading the collection before the commit/close call then let us know, but it won't fetch it in that call, unless you have it set as eager fetch with I think you said it didn't

Mark




Mark I'll post the entire code tomorrow, it's on my machine at my job. But for now, the thing works like this:

Web service calls a method at BO class which call a method at DAO class which calls Hibernate. None of them tries to fetch the collection.

When debbuging, I can see the session.close() been executed and right after Hibernate is fetching the collection.This doesn't happen when I execute a simple local test program. This happens only on web environment.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Web service" Hmm, I was going to say maybe to convert your Domain object to the returning xml that it goes for the Collection. But you say it gets it right before your close, but only in the Web deploy, but not in standalone unit test. So that makes me think, "What's different in your web enviroment" Is the hibernate.cfg.xml different there? Let's try to see if we can figure that out.

Mark
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic