persistence@glassfish.java.net

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

From: Juan Carlos Molina <jcarlosmolina_at_gmail.com>
Date: Fri, 17 Mar 2006 17:19:12 +0100

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