aspose file tools*
The moose likes Object Relational Mapping and the fly likes Lazy loading in One-To-One association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Lazy loading in One-To-One association" Watch "Lazy loading in One-To-One association" New topic
Author

Lazy loading in One-To-One association

Vasily Kukhta
Greenhorn

Joined: Jan 17, 2009
Posts: 23

Hello!

I have an application which uses Hibernate 3.2.5 to work with my database (DBMS: DB2 9.5).

And i have a following problem: when i try to access some property (for example, ID) of an object retrieved from the database with a session.load method, Hibernate begins to fetch eagerly all one-to-one associations of this object, and then tries to fetch all my database into memory recursively through other one-to-one references, until my application crashes with StackOverflowException.

Is there any way to enable lazy loading of one-to-one associations, so they can be initialized only when i hit this association, not some other object property?
Or is there any way to set some sort of "fetch depth" of one-to-one references?

Any information, links, sample code highly appreciated. Thanks in advance!
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

You can just specify lazy in the annotation or hbm file.

@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)

Here's an example from Mapping One to One Associations with Hibernate and JPA Annotations.










Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Yes there is a simple way to achieve this..
<one-to-one name="productBasic" class="ProductBasic" constrained="true" outer-join="false" />

Vasily Kukhta
Greenhorn

Joined: Jan 17, 2009
Posts: 23
Thank you very much for your answers! The problem is solved.

Best regards, Vasily.
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Good to know your problem is solved..

FYI:
If you specify property-ref in your one-to-one association, then lazy loading will not work by any means as this functionality is not yet provided in hibernate.

http://opensource.atlassian.com/projects/hibernate/browse/HHH-3927
Vasily Kukhta
Greenhorn

Joined: Jan 17, 2009
Posts: 23
Sandeep, thank you for your remark! I will keep it in mind.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lazy loading in One-To-One association