• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: Join fetching (non lazy) of an object.

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Address table that has many-to-one relationship to an AddressType table. I want to load (initialize) AddressType for every Address object loaded.

I modified the address.hbm file to look like this

<many-to-one name="addressType" class="dao.beans.AddressType" fetch="join">

When I query by addressID, addressType is initialized fine.

However, when I query for a list of addresses, the addressType is not initialized.

Any hints or tips are greatly appreciated.

Hanna,
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really wanted to have tha relationship not be lazy, the mappign would be

lazy="false"

Anyway, you should always keep your all your mappings as the default of lazy, and only in your use cases when writing your Queries do you set the fetch mode.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically it is like this

all lazy because lets say you have parent to child and child is lazy="false" so then every single time you query for parent you will always get all the child data loaded. And say I 20 parents each with 100 children, and I have a page that lists parents. So I do a query for the 20 parents, but now all the 100 children each get loaded to and sent to the client. But the client for this use case only needs the list of parents. That is a waste of memory and bandwidth.

Now you might have a use case where you need all the children loaded with the parents on a page, in that use case, you run your query where you set the fetchmode to eager (non-lazy).

This design is to minimize the amount of data needed to just what is needed for the use case, and won't waste memory, query time, and bandwidth.

Mark
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic