persistence@glassfish.java.net

Re: question about object relationships

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Wed, 14 Mar 2007 16:08:58 -0800

Try to use IdClass if you use TopLink Essentials - I think there is a bug with
EmbeddedId. You'll also might need to add insertable/updateable false in either
computerId or the relationship field.

regards,
-marina

Martin Bayly wrote On 03/14/07 15:55,:
> Possibly, I've defined the PortPK incorrectly? Can it contain a reference
> to a Computer or should it just have a member of type Long to represent the
> Computer component of the PK?:
>
> @Embeddable
> class PortPK {
> private String type;
> private Long computerId;
> }
>
>
> -----Original Message-----
> From: Martin Bayly [mailto:mbayly_at_telus.net]
> Sent: March 14, 2007 4:48 PM
> To: persistence_at_glassfish.dev.java.net
> Subject: RE: question about object relationships
>
> I think I'm a bit confused about how this would be mapped in JPA
> annotations.
>
> If I understand correctly Jeff's data model is something like:
>
> _COMPUTER_
>
> ID PK
>
> _PORT_
>
> TYPE PK
> COMPUTER_ID PK FK
>
> This means that Port has a composite primary key.
>
> @Embeddable
> class PortPK {
> private String type;
> private Computer computer;
> }
>
> @Entity
> class Port {
>
> @Id
> PortPK portId;
>
> }
>
> @Entity
> class Computer {
>
> @Id
> private Long id;
>
> @OneToMany
> @JoinColumn(name="COMPUTER_ID")
> private Set<Port> ports;
> }
>
> This would represent a uni-directional OneToMany between Computer and Port.
> i.e. A computer knows about it's ports but a port doesn't know about it's
> computer (other than the fact that the port pk includes the computer - this
> is what is confusing me a bit).
>
> I don't think TopLink essentials supports the use of @JoinColumn on a
> uni-directional OneToMany relationship. At least, I think I raised a bug
> about this a while ago, but do not know if that has been addressed.
>
> If this was to be setup as a bi-directional relationship what would it look
> like?
>
> 1. Just drop the @JoinColumn?
> 2. or Add a @ManyToOne annotation to the PortPK Computer field?
> 3. or something else?
>
> Thanks
> Martin
>
> -----Original Message-----
> From: Marina.Vatkina_at_Sun.COM [mailto:Marina.Vatkina_at_Sun.COM]
> Sent: March 14, 2007 3:54 PM
> To: persistence_at_glassfish.dev.java.net
> Subject: Re: question about object relationships
>
> If you start with Java classes, and would like for the provider to generate
> the
> tables for you, you'll need to specify the desired mapping, but if you map
> to an
> existing schema, part of the PK in the Port (e.g. computerId) will be a FK
> to
> the PK in the Computer, and another field (e.g. type) will be just a PK
> field.
> In this case a combination of 2 fields in the Post will guarantee that
> ComputerA
> and ComputerB can have ports of the same type.
>
> HTH,
> -marina
>
> jeff wrote On 03/14/07 14:38,:
>
>>i still don't understand then. if there's a unidirectional relationship
>>from one Computer to many Ports, then Port is only identified by it's
>>type. If that's the case, then it's impossible for ComputerA and
>>ComputerB to have ports of the same type.
>>
>>a Port's ID must somehow incorporate the Computer that it belongs to,
>>and the only way i can see to do that is to have a bi-directional
>>relationship. or, to manually (or automatically) assign each port a
>>unique ID.
>>
>>is that correct?
>>
>>thanks.
>>
>>*/Marina Vatkina <Marina.Vatkina_at_Sun.COM>/* wrote:
>>
>> It should work either way.
>>
>> -marina
>>
>> jeff wrote On 03/14/07 14:14,:
>> > thanks marina, i did see that.
>> >
>> > then i think my question becomes: is it necessary to have a
>> > bidirectional relationship to affect this behavior?
>> >
>> > thanks.
>> >
>> > */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.
>> > >
>> > > 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.
>> > >
>> > > i hope this makes sense.
>> > > thanks!
>> > >
>> > >
>> > >
>> >
>>
> ------------------------------------------------------------------------
>
>> > > Now that's room service! Choose from over 150,000 hotels
>> > > in 45,000 destinations on Yahoo! Travel
>> > > >> to find your fit.
>> >
>> >
>> >
>>
> ------------------------------------------------------------------------
>
>> > Need Mail bonding?
>> > Go to the Yahoo! Mail Q&A
>> >
>> > for great tips from Yahoo! Answers
>> >
>> > users.
>>
>>
>>------------------------------------------------------------------------
>>Don't be flakey. Get Yahoo! Mail for Mobile
>><http://us.rd.yahoo.com/evt=43909/*http://mobile.yahoo.com/mail> and
>>always stay connected
>><http://us.rd.yahoo.com/evt=43909/*http://mobile.yahoo.com/mail> to
>>friends.