• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Hibernate lazy collections: What about the collection elements - are they lazy too?

 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I wonder whether Hibernate creates a Proxy or does a database hit when one try to get an element from a lazy collection.

Any ideas?

Regards,
Darya
 
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
If you have lazy set, then Hibernate will do a hit to the database to get the data when you hit the Collection. However, you can determine how many times it will hit the database. If you set your fetching strategy to subselect, then it will get the data for the collection in one query. If you have no fetching strategy, then it will hit the database for each element in the database, so if there are 10 elements in the Collection that would be 10 seperate queries.

A good understanding of fetching strategies is very important to make sure you are in control of how much Hibernate hits the database.

Mark
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
If you have lazy set, ...
Mark



I hope it does also work for a lazy list. I still need to check out my log file to see the effect of lazy collections w/o and w/ subselect fetching.

Regards,
Darya
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
If you have no fetching strategy, then it will hit the database for each element in the database, so if there are 10 elements in the Collection that would be 10 seperate queries.

Mark



Hibernate always has a default fetching strategy in place. If I use attribute lazy="true" in my collection I can see (log file) that Hibernate triggers ONE select and NOT multiple selects. That one select provides me a result set of all collection element not in form of real object but kind of proxies, right?

Isn't that what subselect fetching also gives to me ?

Regards,
Darya
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I guess I talk about proxies, however I'm not 100% sure . What is the log message that clearly states whether I got a proxy or the real object
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think when it (log file) says: we talk about the real object and not a proxy.

OK, here are my results after adapting my Hibernate Mapping file with subselect and removing again:

When your collection is lazy it doesn't matter whether you have subselect or not. In both cases you have ONE select with a WHERE clause set to the parent's ID. The select is triggered with your first collection.get(0). All other collection.get(1), collection.get(2), ... are initialized w/o doing a select again.

This behavior was the same whether subselect was on or not . Does anyone has an explanation?

Regards,
Darya
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic