I'm trying to do a ManyToMany relationship in JPA 2.0 (JBoss 7.1.1) with an extra column (in bold, below) in the relationship, like:
Employer EmployerDeliveryAgent DeliveryAgent
(id,...) (employer_id, deliveryAgent_id, **ref**) (id,...)
I wouldn't like to have duplicate attributes, so I would like to apply the second solution presented in http://giannigar.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/ . But I can't get it to work, I get several errors like:
- Embedded ID class should not contain relationship mappings (in fact the spec says so);
- In attribute 'employerDeliveryAgent', the "mapped by" value 'pk.deliveryAgent' cannot be resolved to an attribute on the target entity;
- In attribute 'employerDeliveryAgent', the "mapped by" value 'pk.employer' cannot be resolved to an attribute on the target entity;
- Persistent type of override attribute "pk.deliveryAgent" cannot be resolved;
- Persistent type of override attribute "pk.employer" cannot be resolved;
Many people on that link said that it worked fine, so I suppose something is different in my environment, perhaps JPA or Hibernate version. So my question is: how do I achieve such scenario with JPA 2.0 (Jboss 7.1.1 / using Hibernate as JPA implementation)? And to complement that question: should I avoid using composite keys and instead use plain generated id and a unique constraint?
Thanks in advance.
Obs.: I didn't copy my source code here because it is essentially a copy of the one at the link above, just with different classes and attributes names, so I guess it is not necessary.
First of all You need to generate a
EmployerDeliveryAgentPK
class because It has a multiple PK:Next, You need to create a
EmployerDeliveryAgent
class. This class represent many to many table ofEmployer
andDeliveryAgent
:After that, in
Employer
class You need to add:And in
DeliveryAgent
class You need to add:This is all! Good luck!!
OK, I got it working based on the solution available at
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns.
This solution does not generate duplicate attributes on the database, but it does generate duplicate attributes in my JPA entities (which is very acceptable, since you can relay the extra work to a constructor or method - it ends up being transparent). The primary and foreign keys generated in the database are 100% correct.
As stated on the link, I couldn't use @PrimaryKeyJoinColumn and instead used @JoinColumn(name = "projectId", updatable = false, insertable = false, referencedColumnName = "id"). Another thing worth mentioning: I had to use EntityManager.persist(association), which is missing on the example at the link.
So my final solution is:
Both answers from Eric Lucio and Renan helped, but there use of the ids in the association table is redundant. You have both the associated entities and their ids in the class. This is not required. You can simple map the associated entity in the association class with the
@Id
on the associated entity field.The association class
Still need the association PK class. Notice the fields names should correspond exactly to the field names in the association class, but the types should be the type of the id in the associated type.