Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic