Mapping a many-to-many join table with extra column(s) using JPA#

As of 23.02.2010

There seams to be only following two blogs in the web where this is described:

boris.kirzner.info/blog/archives/2008/07/19

sieze.wordpress.com/2009/09/04

But this solution does not work with EclipseLink, because obvioulsy EclipseLink does only allow simple Java types as primary key fields in the Embeddable class.

I am still a newby in JPA and had some difficulties to solve it, so I tried to use some tools:

Database Tools#

Though owned by Oracle now, I still use MySql. My choice for designing tables and relations
is MySql Workbench. You can easily syncronize your model with the database (Menu > Database).
So I "Reverse Engineer..." my few database tables so far into a new model, changed it and "Synchronize Model..." back to the database.
To validate what happens on the database I used additionally Squirrel for SQL.

Eclipse#

In Eclipse you can create a "JPA Project". This comes from a project called Dali and is part of the WTP (Web Tools Plattform).

You can update Eclipse with following update source:
http://download.eclipse.org/webtools/updates/

Please make sure to use the latest Dali update, otherwise it will not create the additional properties in the join table (see below)!

When you created a JPA Project, you can find
Context Menu > New > Entities From Table

Then a wizard comes up, where you can select your table and adjust your mappings. Then Eclipse created all entity classes with all annotations. Perfect stuff - now I don't need to rack my brain about this!
The nice thing is that you can test various models and see how this would look with JPA.

I tried also the model from the links above and this came out.
"color", "isAdmin" and "name" are the (stupid) additional properties
in ProductItem.

Item#

Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.

Product#

Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.

ProductItem#

Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.

ProductItemPk#

Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.Inetone : JPA_ManyToMany - Einfügen eines Plugins fehlgeschlagen: Plugin 'Java2HtmlPlugin' konnte nicht gefunden werden.