Yeah, long story short, if I have the same columns in both tables (and thus
the same fields for the Record key are in both Java entities), it works no
problem.

>From what I gather in my googling of the problem, this is basically a
limitation of JPA 1.0:

if you want to do a relationship on a value that is part of a composite key,
ALL key fields must be present in both classes. They don't need to actually
be @Id fields in both classes... just present to use in the join logic.

So the solution was to add owner and source to the RecordDetails table...
not a big deal but seems weird JPA required that.

But then I didn't design the schema and am by no means a DB expert so
perhaps that should have been the DB design from the beginning.

On Fri, Jul 2, 2010 at 8:23 AM, Josh Peters <jcpet...@gmail.com> wrote:

> I found this article by IBM to be helpful for understanding Hibernate
> + JPA and composite keys:
> http://www.ibm.com/developerworks/opensource/library/os-hibernatejpa/index.html
>
> Short story: you'll have to extract your composite key into its own
> class and then use that as the @Id field.
>
> Good luck.
>
> On Jul 1, 3:20 pm, Sean Comerford <sean.c.comerf...@gmail.com> wrote:
> > Hours of googling have failed so I'm hoping one of you JavaPosse list
> > geniuses can bail me out here :-)
> >
> > I have what I think is a pretty simple relationship between two DB
> tables.
> >
> > Record
> > ---------
> >      int seqId (key)
> >      int owner (key)
> >      String source (key)
> >      ... other non key fields ...
> >
> > RecordDetails
> > -------------------
> >     int genId (key)
> >     int seqId
> >     String detailsText
> >
> > So record has a one to many relationship with record details based on the
> > seqID.
> >
> > The catch here is the primary key in the record table is COMPOSITE
> > consisting of seqId, owner and source (whereas RecordDetails pk is just
> the
> > generated column genId).
> >
> > Starting with just having:
> >
> > @OneToMany
> > @JoinColumn(name="seqId")
> > private List<RecordDetails> details ...
> >
> > I've tried 18 different ways to model this in JPA / Hibernate and after
> much
> > googling still can't get it to work.
> >
> > The jist of my googling seems to be that there's a limitation in JPA 1.0
> in
> > that ManyToOne and OneToMany relationships on foreign keys MUST have the
> > same key columns.
> >
> > My gut feeling is there MUST be a way around this... I'm willing to use
> > Hibernate annotations if necessary.
> >
> > Can anyone help me out?
>
> --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to javapo...@googlegroups.com.
> To unsubscribe from this group, send email to
> javaposse+unsubscr...@googlegroups.com<javaposse%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to javapo...@googlegroups.com.
To unsubscribe from this group, send email to 
javaposse+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to