-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Makeshift solution to lazy 0 to 1 relationship
PostPosted: Tue Mar 21, 2006 11:22 am 
Newbie

Joined: Thu Jun 03, 2004 2:10 am
Posts: 19
Hibernate version: 3.1.2

I just wanted to share a lazy 0 to 1 solution that i've been using. I admit that there must be better solutions out there, but it might be usable by some - and it really takes a few minutes to implement.

You basically set up the mapping files as a one-to-many relationship, set the access as private, and use helper functions to simulate the one-to-one relationship. I haven't done it with two classes with a one-to-one relationship on the primary key, but I assume it will work as well...

For example:

If you have the following entities - Person and Address. A person can have an address, but he/she doesn't have to have one. We want this relationship to be lazy. Hibernate only supports this mode with byte code manipulation - which is fine, but sometimes we don't have that luxury.

In the Person mapping file, we'd set up a one-to-many for the addresses:

Code:
        <set name="addresses" lazy="true" cascade="...">
            <key column="person_id" />
            <one-to-many class="Address" />
        </set>


In the Address mapping file we have the normal mapping depending on a foreign/primary key association. In this example it could be:


Code:
       
        <many-to-one name="person"
            class="Person"
            column="person_id"
            cascade="none"
            ....>
        </many-to-one>


Again, it should work other types of bidirectional mappings that support one-to-many.

In the code that is generated (or if you write the code yourself), you'll have a setter and getter for "addresses". You could make these private if you want because you will never be using them outside the class.

Now add to the Person class two "normal" one-to-one setters/getters:

Code:
    public Address getAddress()
    {
        Set addresses = this.getAddresses();
        if (addresses== null) return null;
        if (addresses.size() == 0) return null;

        return (Address)addresses.iterator().next();
    }

    public void setAddress(Address address)
    {
        Set addresses = this.getAddresses();
        if (addresses == null)
        {
            addresses = new HashSet();
            this.setAddresses(addresses);
        }
        addresses.clear();
        addresses.add(logo);
    }


Now you can use your Person class normally and it should act like it supports a lazy one-to-one relationship.

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 3:15 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
In the example just posted a few days ago (listed below), if you put lazy="proxy" in the <one-to-one mapping on the Person side, it will lazily load the car...

check out this example:

http://forum.hibernate.org/viewtopic.ph ... highlight=

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 3:26 pm 
Newbie

Joined: Thu Jun 03, 2004 2:10 am
Posts: 19
Only if it's set to constrained="true" can hibernate create a proxy in a one-to-one relationship because if there was no address - it couldn't use null as a proxy object.

This means that lazy="proxy" in a one to one relationship only works if the Person *must* have an Address (i.e. the constrained).

At least this is what I understood from the Hibernate documentation (see the docs chapter 5.1.11). It is possible if you use byte-level manipulation.

Has this changed?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 3:37 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
I just tested the sample I gave - it was lazily loaded.
before accessing the CAR from the Person it was NULL, and when I accessed it read from DB, initialized it and viola! - available.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 3:44 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
so having said that - I would claim that
pg60 of reference.pdf:
Quote:
"LAZY
...Note that if constrained="false", proxying is impossible and Hibernate will eager fetch the association!"


is a problem with the docs - since the sample is constrained="false" and it does lazy proxy and not eagerly fetch.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.