aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to implement OneToOne entity without explicit id field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to implement OneToOne entity without explicit id field" Watch "How to implement OneToOne entity without explicit id field" New topic
Author

How to implement OneToOne entity without explicit id field

Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
I want to have 2 tables, say A and B, that are in one-to-one relationship.
To do this in entity class for A I add the following:

@OneToOne
private B b;

This is still not valid in JPA because it requires an explicit id field. But I don't want redundant fields in database.
How can I solve this problem ?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Here's a scenario with an Exam and an ExamDetail:



As you can see, it's a one to one mapping. There does need to be a reference from one table to the other, that's for sure. That's not superfluous - that's necessary!

The code is below. The example comes from my book Hiberante Made Easy. You can find some more information about this example on my website.







Regards!

-Cameron McKenzie

Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
I repeat the following requirement:
>> I don't want redundant fields in database.

In your example the field details_id is redundant. One-to-one relationship can be organized using only id fields of entities Exam and ExamDetails. I want to know how to do this.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Boris Romashov wrote:I repeat the following requirement:
>> I don't want redundant fields in database.


No need to get testy. I'm really only trying to help.

I guess I've always been incorrectly taught that two associated table in a database needed to be referenced through a foreign key. It appears that this 'foreign key' idea that I've been misinformed about is superfluous and redundant.

How do you enforce a constrained association between two tables without an apparently redundant and superfluous foreign key? I'm curious.

Regards,

-Cameron McKenzie
Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
Cameron Wallace McKenzie wrote:
How do you enforce a constrained association between two tables without an apparently redundant and superfluous foreign key?

Each field in the table can be used as foreign key, even primary key. In table A field id can be autoincrement, in table B it should be usual integer that is linked with table A.

The same happens when a table builds many-to-many relationship. It doesn't have any special primary keys. We use pair of foreign keys as a natural primary key. I want the same but for one foreign key instead of two.

Is it impossible in JPA ?
 
jQuery in Action, 2nd edition
 
subject: How to implement OneToOne entity without explicit id field