• 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

Id not availble in lazy loaded associate objects

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the follow object:



eventId and userId are fks in slideshow table. By default User and Event should be lazy loaded to SlideShow by proxy. Hence, I should be able to get eventId and userId from slideShow.getEvent().getEventId() However, the eventId is null when I step through the proxy object in debugger. Same is true for userId. Am I missing something? Should the proxy object has the id of the associate object loaded lazily? How does one retrieve it? Thank you for all your help.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is that the code that gives you and event id?

If getEvent works without giving you a lazyloading exception, then you should get the eventId. Calling getEvent within an open session will force Hibernate to load the entity for you.
 
Johnny Cheng
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's the code. Does whether lazy initialization happened depends on the default of the mapping? ie. @ManyToOne default lazy so it will get exception; @OneToOne won't because default is eager. What determine if hibernate will run the extra select to get the association. I am still a little confused with any solid rule.

Thank you very much for your answer. I am a big fan of your book. It is my "the book" for for me in using hibernate. I get more every time I read it over.
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming everything is lazy, the slideshow will be pulled from the db when it is requested the first time, but the event will not. When the event is first called with a getter, it should be pulled with all of its attributes. So, if getEvent() doesn't return null, the event must have been loaded. If getEvent().getEventId() returns null, the event is not null, but for some reason the eventId is.

Thanks for the kind words about the book!

-Cameron McKenzie
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic