aspose file tools*
The moose likes Object Relational Mapping and the fly likes Id not availble in lazy loaded associate objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Id not availble in lazy loaded associate objects " Watch "Id not availble in lazy loaded associate objects " New topic
Author

Id not availble in lazy loaded associate objects

Johnny Cheng
Greenhorn

Joined: Nov 05, 2009
Posts: 5
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

Joined: Aug 26, 2006
Posts: 4968



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

Joined: Nov 05, 2009
Posts: 5
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

Joined: Aug 26, 2006
Posts: 4968

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Id not availble in lazy loaded associate objects
 
Similar Threads
Annotations doubt
Outer join between 3 entities in HQL
ManyToMany assoicate delete join table entry
JPA Mapping exception repeated column in mapping help requested
JPA ManyToOne composite foreign key reference for optional target