0

Im having trouble using composite primary keys with JPA EclipseLink. The problem is when I theres a foreign key that is the primary key of another table. I have this simple scenario.

User

public class Users implements Serializable {
...
private Collection<UserCompany> userCompanyCollection;
@JoinColumn(name = "user_roles", referencedColumnName = "user_role_id")
@ManyToOne(optional = false)
private UserRoles userRoles;
...
}

User Roles

public class UserRoles implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected UserRolesPK userRolesPK;
…
}

User Roles PK

@Embeddable
public class UserRolesPK implements Serializable {
@Basic(optional = false)
@Column(name = "user_role_id")
private int userRoleId;
@Basic(optional = false)
@NotNull
@Column(name = "user_role_company_id")
private int userRoleCompanyId;
...
}

With that objects, I get this exception:

Caused by: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field userRoles] from the entity class [class jpa.Users] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
at org.eclipse.persistence.exceptions.ValidationException.incompleteJoinColumnsSpecified(ValidationException.java:1805)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.getJoinColumnsAndValidate(MappingAccessor.java:575)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.getJoinColumns(MappingAccessor.java:525)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOneToOneForeignKeyRelationship(ObjectAccessor.java:629)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ObjectAccessor.processOwningMappingKeys(ObjectAccessor.java:686)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ManyToOneAccessor.process(ManyToOneAccessor.java:119)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processOwningRelationshipAccessors(MetadataProject.java:1432)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1667)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:521)
at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:526)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1320)
... 36 more
|#]

Thanks in advance for all the help.

Regards, Daniel

Daniel Rojas
  • 407
  • 2
  • 5
  • 16

1 Answers1

0

JPA requires using the full primary key in relationship mappings, which is why it doesn't like your mapping - you are not using the user_role_company_id pk field. If user_role_id is enough to uniquely identify userRoles, then it should not be using a composite key and instead only use the single field.

EclipseLink is capable of mapping foreign keys to non or incomplete ID fields, but I recommend against it: Entities are cached on their primary keys, so resolving relationships may require unnecessary database queries even when the entity is in the cache already. Mapping it requires using a customizer to either create or modify the mapping. An example using a customizer is here http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

Chris
  • 20,138
  • 2
  • 29
  • 43