Yes, the best way to map such relationship is via a composite PK on port. If you
use a single (e.g. generated) id, you'll get OneToMany relationships, but not
really a case when the many side can't exist without the one side.
Please check order & lineitem entities in the orderapp in the JavaEE tutorial -
the example should work (if not - please file a bug as it'd be a regression ;) ).
thanks,
-marina
Martin Bayly wrote On 03/14/07 16:28,:
> 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
> <http://answers.yahoo.com/dir/index;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx?link=ask&sid=396546091>
> for great tips from Yahoo! Answers
> <http://answers.yahoo.com/dir/index;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx?link=ask&sid=396546091>
> users.
>