• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help me understand this key mapping in "Hibernate in Action"

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On pg 223/224 of "hibernate in Action",

Tables:

USER {
user_id <pk>
firstname
...
}

ADDRESS {
address_id <pk> <fk>
street
..
}

<class name="Address" table="ADDRESS">
<id name="id" column="ADDRESS_ID"
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
...
<one-to-one name="user"
class="User"
constrained="true"/>
</class>

There are two things I don't get ---

1. For ADDRESS table, the address_id is its own PK, how can it be a FK as well ? If it is a FK to USER table, then it means in USER table we have such a PK matching it. But address_id is totally different from user-id. So why ?

2. secondly, I don't understand

<generator class="foreign">
<param name="property">user</param>
</generator>

it only says this "address_id" is a foreign, but doesn't describe it as a PK. what does

<param name="property">user</param>

mean ? confusing syntax
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should have posted this in the ORM forum.

Originally posted by ben oliver:
But address_id is totally different from user-id.



Incorrect. Mentally replace in figure 6.8 ADDRESS_ID with USER_ID (no the figure isn't wrong � this is just to help you along).
This is covering the bizarre situation were USER_ID=999 will have ADDRESS_ID=999.
So the mapping is telling Hibernate to get the primary key value for ADDRESS_ID from the parent record's USER_ID. Some legacy databases may use this "questionable" trick for some one-to-one mappings.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peer Reynders:
Should have posted this in the ORM forum.



Incorrect. Mentally replace in figure 6.8 ADDRESS_ID with USER_ID (no the figure isn't wrong � this is just to help you along).
This is covering the bizarre situation were USER_ID=999 will have ADDRESS_ID=999.
So the mapping is telling Hibernate to get the primary key value for ADDRESS_ID from the parent record's USER_ID. Some legacy databases may use this "questionable" trick for some one-to-one mappings.



sounds interesting. So this "ADDRESS_ID" is actually a "user_id", it is the set of user_ids who have Address feature. If I have 1000 users and each one has a unique user_id, then if each user has an "Address" table associated with it, then this "ADDRESS_ID" is just same as these 1000 user_ids. Make sense ? If only 950 users have Address, then these ADDRESS_ID has 950 entries. Well, that doesn't sound possible if it is a true one-to-one.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value of the "ADDRESS_ID" primary key field of the address record is identical to the value of the "USER_ID" primary key field of the user record that the address record belongs to.

i.e. a user doesn't have an Address table associated with it � it has a single record in the Address table associated with it.

Originally posted by ben oliver:
If only 950 users have Address, then these ADDRESS_ID has 950 entries. Well, that doesn't sound possible if it is a true one-to-one.



The term one-to-one doesn't tell you whether the relationship is mandatory or optional.
Figure 6.8 seems to be a home-baked notation which does not directly express mandatory/optional roles (it's neither Barker Entity Relationship (ER), Information Engineering (IE), or Integrated DEFinition 1 eXtended (IDEF1FX) notation). However it does indicate the foreign key constraint on the address side but no foreign key constraint on the user side � this implies that the existence of a user record is mandatory for an address record, while at the same time the existence of an address record is optional for a user record - this is a one-to-one relationship where the user record doesn't require an address record.

In fact, as you can only insert one record at a time, it's impossible for referential integrity to enforce a one-to-one relationship that is mandatory on both ends � you would have to wrap the hole thing in a transaction and check before the end whether both records exist and force a rollback if they do not.
 
reply
    Bookmark Topic Watch Topic
  • New Topic