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

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

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
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 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
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic