Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Id not availble in lazy loaded associate objects

 
Johnny Cheng
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic