persistence@glassfish.java.net

RE: question about object relationships

From: jeff <jeffrey.blattman_at_yahoo.com>
Date: Fri, 16 Mar 2007 09:18:27 -0700 (PDT)

hi martin,

i am letting toplink create the schema, so i am not looking it at from the standpoint of an existing db schema.

what i have now, that appears to be working is that i defined a bidirectional relationship between Computer and Port, where Computer is one to many and Port is many to one. note: the schema created by toplink for this is quite complex. should be a good stress test for a database :)

as i mentioned previously, i think another way to solve this would be to use a unidirectional relationship between Computer and Port, and simply add an auto-generated key to Port to ensure each one is unique, since the Port's type alone isn't enough.

i hope this answered your question. this is all new to me, as you can tell my the # of posts i've been making :)

Martin Bayly <mbayly_at_telus.net> wrote: v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} st1\:*{behavior:url(#default#ieooui) } So are you using a composite primary key on Port or did you just give it a standard primitive unique identifier and use a FK mapping to Computer?
   
  e.g. did you do something like this?
   
  @Entity
  class Port {
      @Id
      private Long id;
   
      @ManyToOne
      Computer computer
   
      private String type;
  }
   
  I kind of got interested in this whole thread because I’ve been reading Eric Evans Domain Driven Design and in his book he talks about the idea of Aggregate Entities which have a root and children. In object modeling terms only the root entity is addressable outside the aggregate and child entities can therefore be referenced using some kind of local identity (local to the root instance).
   
  This seemed to fit neatly into your description of the Computer and its ports. Reading between the lines, it’s also quite common in such models for the relationship from the root to the children to be unidirectional because no-one ever asks for a child and then asks what its root is. You always get the root and ask for its children.
   
  However, I was having a hard time understanding how I would represent such a model in JPA terms. But I think the composite key with a uni-directional OneToMany would be the way to go if it was possible to represent that in JPA/Toplink.
   
  Cheers
  Martin
   
      
---------------------------------
  
  From: jeff [mailto:jeffrey.blattman_at_yahoo.com]
 Sent: March 14, 2007 5:14 PM
 To: persistence_at_glassfish.dev.java.net
 Subject: Re: question about object relationships
  
   
  just fyi, i am not using an existing schema, i am allowing toplink to create it for me.
 
 what i did that that seems to work so far is to set up a bidirectional relationship. i am not really happy about this though, because really i only want the direction to be one way: given a Computer i want to know the Ports, but i'd never look up the Computer given a Port.
 
 anyway, thanks!
 
 Marina Vatkina <Marina.Vatkina_at_Sun.COM> wrote:
  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
 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 /* 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
>> and
>>always stay connected
>> to
>>friends.
 
 

     
    
    
---------------------------------
  
  Need Mail bonding?
 Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.
  
  
 
 
---------------------------------
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.