Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement OneToOne entity without explicit id field

 
Boris Romashov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic