users@glassfish.java.net

Re: JPA relationship pattern question

From: <glassfish_at_javadesktop.org>
Date: Fri, 04 May 2007 13:41:11 PDT

Hi Laird,

> > I'd define the pk on partyId and postalAddressId,
> b/c
> > these values
> > identify the relationship btw Party and
> > PostalAddress.
>
> Well, no. A Party may put the same address under two
> different types. Joe (party id=35) calls 123
> Anywhere Street (id=4) his "preferred" and his
> "mailing" address. Those two records look like
> this:
>
> 35 | "preferred" | 4
> 35 | "mailing" | 4
>
> Note that 35 and 4 are common here and hence can't be
> used as the primary key.
>

Agreed. I meant that even though the field type is logically part of
the primary key, you might be able to not include it into the pk of the
association entity, but rather treat it as an "attribute" of the
relationship. The logic of managing type in the association lookup
could be implemented in a business method, like I explained before. My
previous statement was meant it in this direction.
 
> > This is an overlapping pk/fk situation. In this
> case,
> > you'd need to
> > map PartyPostalAddressBinding's relationship
> fields
> > as
> > updatable=false.
>
> Something looks interesting here. So you're saying
> to "double up" on my properties, i.e. do something
> like this (I'll just do it for the PartyEntity
> property below):
> [code]// inside the binding class
> @ManyToOne...
> public PartyEntity getPartyEntity() ...
>
> @WhateverTheAnnotationIsForARegularOldJoin(updatable=f
> alse)
> public long getPartyEntityID() ...
> [/code]
> ...? Interesting. Would I also have to say
> insertable=false?
>

I was thinking of the following:

@Entity public class PartyPostalAddressBinding {
...
  @Id @Column(name="PARTY_ID")
  public long getPartyId() {..}
  @Id @Column(name="POSTAL_ADDRESS_ID")
  public long getPostalAddressId() {..}
  public String getType() {..}

  @ManyToOne
  @JoinColumn(name="PARTY_ID", insertable="false", updatable="false")
  public PartyEntity getPartyEntity() {}
...
}

But that would mean that the primary key in your entity (partyId,
postalAddressid) is different from the primary key of the mapped
table, which must be on (PARTY_ID, POSTAL_ADDRESS_ID, TYPE). Or you at
least must have a unique index on the three columns. This is certainly
a problem... Not sure if you can make this work.

Looks like you have to put the type attribute into both PARTY and
POSTALADDRESS tables, and define a composite primary key, and then map
the relationship to a join table containing columns (PARTY_ID,
PARTY_TYPE, POSTALADDRESS_ID, POSTALADDRESS_TYPE).

Difficult problem. Good luck,

-- markus.
[Message sent by forum member 'mf125085' (mf125085)]

http://forums.java.net/jive/thread.jspa?messageID=215623