wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes help me understand this key mapping in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "help me understand this key mapping in "Hibernate in Action"" Watch "help me understand this key mapping in "Hibernate in Action"" New topic
Author

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

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 375
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
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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

Joined: Mar 28, 2006
Posts: 375
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

Joined: Aug 19, 2005
Posts: 2922
    
    5
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help me understand this key mapping in "Hibernate in Action"