persistence@glassfish.java.net

Re: question about object relationships

From: Markus Fuchs <Markus.Fuchs_at_Sun.COM>
Date: Wed, 14 Mar 2007 15:18:27 -0700

Hi Jeff,

Marina is correct. Additional information about your problem:

Marina Vatkina wrote:
> Look at the orderapp in the JavaEE tutorial - it has orders and lineitems with
> overlapping PK/FK mapping. This is most probably what you need.
>
> regards,
> -marina
>
> jeff wrote On 03/14/07 11:25,:
>
>> in a one to many (or 1-1) relationship, i want the many's ID to
>> incorporate it's one's ID.
>>
>> for example, if a have a Computer that has Ports. each port has a type,
>> say "USB", "serial", etc. furthermore, say i can only have one of each
>> type of port in a particular Computer.
>>
>> many Computers can have a Ports of the same type. how do i represent
>> this? what is the ID for Port? the type is enough to ID it within the
>> Computer, but i don't know how to represent this in JP. i think i want
>> the Port's ID to incorporate the Computer's ID.
>>
Check out 2.1.4 "Primary Keys and Entity Identity" in the JPA spec. In
particular look for composite primary keys (see below).
>> in the database, i think this would mean that the table for Port has a
>> foreign key pointing back to the the ID column of the table for Computer.
>>
>>
You could model this situation using a composite primary key in the Port
object, and the relationship Computer <-> Port could be modeled in the
database like:

CREATE TABLE COMPUTER (
ID VARCHAR(32) PRIMARY KEY;
...
);

CREATE TABLE PORT (
ID VARCHAR(32);
COMPUTER_ID VARCHAR(32);
...
PRIMARY KEY(ID, COMPUTER_ID);
FOREIGN KEY (COMPUTER_ID) REFERENCES COMPUTER(ID);
);

HTH,

-- markus.

>> i hope this makes sense.
>> thanks!
>>
>>
>> ------------------------------------------------------------------------
>> Now that's room service! Choose from over 150,000 hotels
>> in 45,000 destinations on Yahoo! Travel
>> <http://travel.yahoo.com/hotelsearchpage;_ylc=X3oDMTFtaTIzNXVjBF9TAzk3NDA3NTg5BF9zAzI3MTk0ODEEcG9zAzIEc2VjA21haWx0YWdsaW5lBHNsawNxMS0wNw--
>>
>>> to find your fit.
>>>