0

我在使用 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
|#]

提前感谢所有帮助。

问候,丹尼尔

4

1 回答 1

0

JPA 要求在关系映射中使用完整的主键,这就是它不喜欢您的映射的原因 - 您没有使用 user_role_company_id pk 字段。如果 user_role_id 足以唯一标识 userRoles,则不应使用复合键,而应仅使用单个字段。

EclipseLink 能够将外键映射到非或不完整的 ID 字段,但我建议不要这样做:实体缓存在它们的主键上,因此即使实体已经在缓存中,解析关系也可能需要不必要的数据库查询。映射它需要使用定制器来创建或修改映射。使用定制器的示例在这里 http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

于 2013-02-14T03:43:48.683 回答