|
|
Re: foreign keys in composite primary keys [message #381661 is a reply to message #381656] |
Thu, 25 September 2008 14:31 |
Knut Wannheden Messages: 298 Registered: July 2009 |
Senior Member |
|
|
Hi James,
First, thanks for the quick reply.
James wrote:
> For a primary key composed of a foreign key from a ManyToOne in JPA 1.0
> you need to map the foreign key field twice. With a basic @Id mapping
> and with the @ManyToOne, the @ManyToOne should be set read-only
> (insertable, updatable=false, or use @PrimaryKeyJoinColumn).
>
That's what I gathered from the article. With EclipseLink I could
however not get the @PrimaryKeyJoinColumn approach to work, while the
other approach also worked well with the insertable=false and
updatable=false on the @Id annotated field.
> In EclipseLink you don't really need to add the duplicate basic Id
> mapping, you could instead use a DescriptorCustomizer to add the foreign
> key field to the ClassDescriptor's primary key fields.
>
Can you expand with an example on this? I tried with adding the
@Customizer annotation and removing both the @IdClass and @Id
annotations, but EclipseLink then complained with the following error
even before the customizer got called:
Internal Exception: Exception [EclipseLink-7150] (Eclipse Persistence
Services - 1.0.1 (Build 20080905)):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: Invalid composite primary key specification. The
names of the primary key fields or properties in the primary key class
[com.foo.model.ObjectKey$ObjectKeyId] and those of the entity bean class
[class com.foo.model.ObjectKey] must correspond and their types must be
the same. Also, ensure that you have specified id elements for the
corresponding attributes in XML and/or an @Id on the corresponding
fields or properties of the entity class.
at
org.eclipse.persistence.exceptions.PersistenceUnitLoadingExc eption.exceptionSearchingForPersistenceResources(Persistence UnitLoadingException.java:121)
at
org.eclipse.persistence.jpa.PersistenceProvider.createEntity ManagerFactory(PersistenceProvider.java:117)
at
org.eclipse.persistence.jpa.PersistenceProvider.createEntity ManagerFactory(PersistenceProvider.java:64)
at com.foo.model.Main.main(Main.java:12)
With just the @IdClass annotation I get this, also without the
customizer ever being called:
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence
Services - 1.0.1 (Build 20080905)):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class com.foo.model.ObjectKey] has
no primary key specified. It should define either an @Id, @EmbeddedId or
an @IdClass. If you have defined PK using any of these annotations then
please make sure that you do not have mixed access-type (both fields and
properties annotated) in your entity class hierarchy.
at
org.eclipse.persistence.exceptions.PersistenceUnitLoadingExc eption.exceptionSearchingForPersistenceResources(Persistence UnitLoadingException.java:121)
at
org.eclipse.persistence.jpa.PersistenceProvider.createEntity ManagerFactory(PersistenceProvider.java:117)
at
org.eclipse.persistence.jpa.PersistenceProvider.createEntity ManagerFactory(PersistenceProvider.java:64)
at com.foo.model.Main.main(Main.java:12)
> For a m-m with additional data, the best way is to define a class for
> the join table as the article suggests.
>
OK. I would then also here want to take the same approach as for the
above problem.
--knut
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03631 seconds