-->
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.  [ 13 posts ] 
Author Message
 Post subject: One-to-One with "Reverse" Foreign Key?
PostPosted: Thu Jan 08, 2004 11:49 am 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
I've got a one-to-one relationship, like a Customer and its Address:

I'd like the object graph to be navigable only from Customer to its Address (the other way makes no sense).

Unfortunately, the existing database model (which cannot be changed) implements the relationship as a "fake" foreign key from the Address to the primary key of the Customer. Fake, because there is actually no foreign key constraint applied, but it could.

Is this situation mappable and how?

I started with <one-to-one name="address" class="Address" cascade="all" outer-join="auto" constrained="false"/> in the Customer.hbm.xml. On the side of the Address, I don't want any getters/setters, so I can't define anything in the mapping descriptor, right?

Regards,

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 11:55 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You could try a <many-to-one> in the Customer mapping or use a component.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:10 pm 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
Can components also exist in other tables (I tought only in the same table as the parent)? Could you give an example, please?

Regards,

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:18 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Eh, I was thinking about composite-elements. Forget that :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:34 pm 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
It is my understanding that composite elements only have to do with Collections, respectively associations with greater multiplicities than one.

In my case, I've got only a One-to-One relationship: A Customer can have only one Address, and an Address only belongs to one Customer.

Could you give an example of how to use composite elements in my case, please?

Regards,

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:36 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Components wont work in your case, my fault. Try a <many-to-one>

Code:
<class name="Customer">
    <many-to-one name="address"
                          class="Address"
                          column="CUSTOMER_ID"
                          not-null="true"/>
...
</class>

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:43 pm 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
Regarding your "many-to-one"-hint in your first reply:

<many-to-one/> expects a mapped column on the "many" end.

If I were able to tie the many end to the Customer, from the view of the Customer the multiplicity would be 1 (and the other direction is not navigable). I could substitute one-to-one by many-to-one in this case.

Unfortunately, my mapped column (customer_id) is on the Address end. I can't tie the "many" end to the Address, because then the multiplicity would be n (many) from view of the Customer.

Is this correct?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:47 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The <many-to-one> on the Customer does what you want.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:52 pm 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
I just tried your example, but I got an "ORA-00904: invalid column name". I think this happens because column specifies a foreign key column on the parent side, not on the associated side.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:55 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
Unfortunately, the existing database model (which cannot be changed) implements the relationship as a "fake" foreign key from the Address to the primary key of the Customer.


You have to put this column (that is in the ADDRESS table) as the column for the many-to-one. I don't know if its really called CUSTOMER_ID, but this is what you described. If it doesn't have a foreign key constraint, you have a broken relational model, but as long as it has the Customers primary key in that column, it should at least work.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 7:34 pm 
Newbie

Joined: Fri Mar 04, 2005 2:02 pm
Posts: 18
This is actually not true. The column gets created in the CUSTOMER table not the ADDRESS table.

How can you reverse this situation when you have a 1->0or1 OR a 1->M unidirectional relationship?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 7:39 pm 
Newbie

Joined: Fri Mar 04, 2005 2:02 pm
Posts: 18
to follow up to my previous post...this can be seen in the docs too. How can I reverse this situation and get the foreign key in the Adress table? A <one-to-many> tag outside a collection type thing?

Thanks!

7.2.2. one to one

A unidirectional one-to-one association on a foreign key is almost identical. The only difference is the column unique constraint.

<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>
</class>

create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 2:21 pm 
Newbie

Joined: Fri Mar 04, 2005 2:02 pm
Posts: 18
Hate to bottom post,

but does not one know how to do a one-to-one unidirectional relationship with the foreign key in the 'child'?


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