在EclipseLink上使用Compsite键的异常

时间:2013-02-13 17:22:52

标签: jpa eclipselink composite-key

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

提前感谢所有帮助。

此致 丹尼尔

1 个答案:

答案 0 :(得分:0)

JPA要求在关系映射中使用完整的主键,这就是为什么它不喜欢你的映射 - 你没有使用user_role_company_id pk字段。如果user_role_id足以唯一地标识userRoles,那么它不应该使用复合键,而只使用单个字段。

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