Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to implement OneToOne entity without explicit id field

 
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 ?
 
author and cow tipper
Posts: 5000
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
Posts: 5000
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 ?
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic