我在使用 JPA EclipseLink 的复合主键时遇到问题。问题是当我有一个外键是另一个表的主键时。我有这个简单的场景。
用户
public class Users implements Serializable {
...
private Collection<UserCompany> userCompanyCollection;
@JoinColumn(name = "user_roles", referencedColumnName = "user_role_id")
@ManyToOne(optional = false)
private UserRoles userRoles;
...
}
用户角色
public class UserRoles implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected UserRolesPK userRolesPK;
…
}
用户角色 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;
...
}
有了这些对象,我得到了这个异常:
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
|#]
提前感谢所有帮助。
问候,丹尼尔