Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » foreign keys in composite primary keys
foreign keys in composite primary keys [message #381404] Thu, 25 September 2008 11:52 Go to next message
Knut Wannheden is currently offline Knut Wannheden
Messages: 296
Registered: July 2009
Senior Member
Hi all

I'm new to JPA and EclipseLink and hit some problems regarding primary
keys composed of one or multiple foreign keys.

1. What's the recommended EclipseLink approach when an entity with a
composite primary key also has relations (e.g. many-to-one) using a
constituent of the primary key as foreign key? I found a nice summary of
the problem on this wiki page:
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_S equencing#Primary_Keys_through_OneToOne_Relationships.
There are actually some references to how this should be done in
EclipseLink, but I couldn't quite figure it out.

2. A related but different problem arises with join tables where one
would like to add additional columns to the table. Also this is
described on the wiki:
[http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany]. Does
EclipseLink have something to offer here?

Thanks

--knut
Re: foreign keys in composite primary keys [message #381656 is a reply to message #381404] Thu, 25 September 2008 13:30 Go to previous messageGo to next message
James is currently offline James
Messages: 272
Registered: July 2009
Senior Member
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).

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.

For a m-m with additional data, the best way is to define a class for the
join table as the article suggests.

-- James
Re: foreign keys in composite primary keys [message #381661 is a reply to message #381656] Thu, 25 September 2008 14:31 Go to previous messageGo to next message
Knut Wannheden is currently offline Knut Wannheden
Messages: 296
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
Re: foreign keys in composite primary keys [message #381663 is a reply to message #381661] Mon, 29 September 2008 13:25 Go to previous messageGo to next message
James is currently offline James
Messages: 272
Registered: July 2009
Senior Member
Yes, the JPA meta-data, defaults and validation is processed before the
DescriptorCustomizer, so avoiding duplicate mappings will be a little
tricky. Basically you will need to set the primary key to a single basic
field to get past the validation, the fix things up with the customizer.

Support for @Id on ManyToOne and OneToOne should be added soon in the
EclipseLink 1.1 stream, so you could also wait for that, (or just define
the duplicate mappings for now).

-- James
Re: foreign keys in composite primary keys [message #381676 is a reply to message #381663] Thu, 02 October 2008 07:50 Go to previous message
Knut Wannheden is currently offline Knut Wannheden
Messages: 296
Registered: July 2009
Senior Member
Hi James

Thanks for the tip. I think I'll stay with the duplicate mappings for
now and wait for the 1.1 release.

--knut

James wrote:
> Yes, the JPA meta-data, defaults and validation is processed before the
> DescriptorCustomizer, so avoiding duplicate mappings will be a little
> tricky. Basically you will need to set the primary key to a single
> basic field to get past the validation, the fix things up with the
> customizer.
>
> Support for @Id on ManyToOne and OneToOne should be added soon in the
> EclipseLink 1.1 stream, so you could also wait for that, (or just define
> the duplicate mappings for now).
>
> -- James
>
>
Previous Topic:EclipseLink with JBoss and MySQL
Next Topic:Content provider with a long loading time
Goto Forum:
  


Current Time: Sun Sep 21 08:19:52 GMT 2014

Powered by FUDForum. Page generated in 0.01778 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software