persistence@glassfish.java.net

Re: What if part of the Primary Key is a Foreign Key?

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Fri, 17 Mar 2006 09:52:42 -0800

Juan Carlos,

Java EE 5 tutorial has an OrderApp example with overlapping PK/FK combination.
Currently there is an open bug (issue 351) on this mapping, but it's working
on the beta build.

Regards,
-marina

Juan Carlos Molina wrote:
> Dear Glassfish people,
>
> I'm taking my first steps at developing EJB30 apps and I've found
> something I still don't know how to solve. Hope you can help me with this.
>
> My problem is that I have an entity with a compound PK ... and one of
> its elements is also the foreing key to a related entity.
>
> Take, for instance, the nice example in
> https://glassfish.dev.java.net/javaee5/persistence/persistence-example.html and
> try to have "Order" identified both by "ORDER_ID" and "CUSTOMER_ID".
>
> I'm using an "OrderPK" class (annotated with @Embeddable) to map the
> compound PK of the "Order" entity, in which I have a property of type
> "OrderPK" annotated with @EmbeddedId.
>
> I just could not make it work. I ended up having:
>
> "CUSTOMER_ID" as a column in the "ORDER_TABLE" table *but* not making
> part of its PK,
>
> OR
>
> the "CUSTOMER_ID" column as part of the PK of the "ORDER_TABLE" table
> *but* could not use @JoinColumn in the "Order" entity (it would not
> deploy) to annotate the "getCustomer" getter.
>
>
> I'd appreciate if you could shed some light on this.
>
> Thanks a lot in anticipation.
>
> Best regards,
>
> Juan Carlos Molina
>
>