-->
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.  [ 6 posts ] 
Author Message
 Post subject: one-to-one relashionship without shared primary key
PostPosted: Thu Aug 16, 2007 2:22 pm 
Newbie

Joined: Thu Aug 16, 2007 2:03 pm
Posts: 6
Hello.

Is there any way to map a one-to-one relashionship via just a foreign key, not a shared primary key?

DB schema is like this:

Code:
TABLE whole(
     id_whole INTEGER NOT NULL,
     ...other attributes go here....,
     PRIMARY KEY(id_whole)
);

TABLE part1(
     id_part1 INTEGER NOT NULL,
     id_whole INTEGER NULL,
     ...other attributes go here....,
     PRIMARY KEY(id_part1),
     FOREIGN KEY(is_whole) REFERENCES whole(id_whole)
);

and the id_whole in part1 table is unique via index.

and object model is like this:

Code:
class Whole{
    Integer idWhole;
     ....... other attributes .....
     Part1 part1;
}

class Part1{
    Integer idPart1;
     ....... other attributes .....
}


Yes, I know, the shared primary key is the right (or at least preferred) way to do this, and I know the foreign key does not determine multiplicity and so on.
But I have this DB schema and have no control over it.

So is there any way to achive automatic association here?

So far the only way I see to do this is to map this as many-to-one via Set or whatever container I can use, and implement public get/setPart1 methods in the Whole class so they use this container instead of a plain Part1 variable.
But this is definitely not the elegant solution, which complicates the Whole class, and this is obviously a tool-dictated solution, which I tend to consider a bad thing.

Are there any practical ways?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 2:50 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Use a many-to-one with unique="true".
From the hibernate docs:
Quote:
7.4.2. one to one
A bidirectional one-to-one association on a foreign key is quite common.

Code:
<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <many-to-one name="address"
        column="addressId"
        unique="true"
        not-null="true"/>
</class>

<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
   <one-to-one name="person"
        property-ref="address"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 4:57 pm 
Newbie

Joined: Thu Aug 16, 2007 2:03 pm
Posts: 6
No, this is completely different case.

In this example Person (whole) have a foreign key for Address (part), and my problem goes other way around.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 6:00 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
What about this?
Code:
<class name="Whole">
    <id name="id" column="id_whole">
        <generator class="native"/>
    </id>
   <one-to-one name="Part1"
        property-ref="whole" cascade="all"/>
</class>

<class name="Part1">
    <id name="id" column="id_part1">
        <generator class="native"/>
    </id>
    <many-to-one name="whole"
        column="id_whole"
        unique="true"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 1:58 am 
Newbie

Joined: Thu Aug 16, 2007 2:03 pm
Posts: 6
That's more like it, but this is bidirectional, while initially I wanted uni-directional - I don't need a reference to Whole in Part, I like keeping all things as simple as they can be.

I'm aware of this method but dismissed it for being bi-directional. Now I'm going to take a second thought about it.

Thanks for pointing this mapping out.

But still, is there any way to make this relashionship uni-directional (from Whole to Part)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 7:51 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
How about using a cross reference table?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.