users@glassfish.java.net

Re: _at_ManyToOne when Foreign Key is also Primary Key

From: <glassfish_at_javadesktop.org>
Date: Thu, 21 Oct 2010 15:33:59 PDT

It looks like there are a couple of problems here.

I don't know whether these are typos here or bugs in implementation, however the Card and ClientCard entities refer to a car_id_card column, but you show the Card and ClientCard tables as having cli_id_card columns.

The ClientCardPK class shouldn't be using @Column. It is the entity using it that does the mapping.

@PrimaryKeyJoinColumns is intended to be used where the entire primary key serves as a foreign key to the target. This is the case for one-to-one PK mappings and for secondary table mappings. You should use the JoinColumn(s) annotation(s) to override FK mappings for ManyToOne relationships.

In JPA 1.0 when using JoinColumns to handle an overlapping PK/FK mapping as would result here, you need to indicate whether the PK or the JoinColumn controls the write to the database. In JPA 1.0, the way to handle this is by mapping one of them as insertable=false, updatable=false. IIRC, the convention with Hibernate is to map the JoinColumn as insertable=false, updatable=false.
[Message sent by forum member 'ldemichiel']

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