2

我正在尝试与额外的列实现多对多关系,但gettig低于错误。有人可以解释为什么会发生以下错误以及如何解决?

Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.ValidationException Exception Description: The @JoinColumns on the annotated element [field extension] from the entity class [class com.polycom.cloudAxis.model.ip.ProfileExtensionMapping] 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.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1541) at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1532) at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:235)

轮廓

@Entity
@Table(name = "profile")
public class Profile extends AbstractPersistable<Long> {

private static final long serialVersionUID = 1L;

@Column(name = "instanceType")
private InstanceType instanceType;

@Column(name = "isDefault")
private boolean isDefault;

@Column(name = "profileType")
private ProfileType profileType;

@OneToMany(mappedBy = "profile")
private Set<ProfileExtensionMapping> profileExtensionMappings;

@Id
@Column(name = "profile_id")
private Long profile_id;

}

扩大:

@Table(name = "extension")
@Entity
public class Extension extends AbstractPersistable<Long> {

private static final long serialVersionUID = 1L;

@Lob
@Basic(optional = false)
@Column(name = "json")
private byte[] json;

@Id
@Column(name = "extension_id")
private Long extension_id;

public Long getExtension_id() {
    return extension_id;
}

public void setExtension_id(Long extension_id) {
    this.extension_id = extension_id;
}

@OneToMany(mappedBy = "extension")
private Set<ProfileExtensionMapping> profileExtensionMappings;

}

ProfileExtension映射:

@Entity
@Table(name = "profile_extension_mapping")
@IdClass(ProfileExtensionPK.class)
public class ProfileExtensionMapping extends AbstractPersistable<Long> {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "profile_id", insertable = false, updatable = false)
private Long profile_id;

@Id
@Column(name = "extension_id", insertable = false, updatable = false)
private Long extension_id;

@ManyToOne
@JoinColumns({ @JoinColumn(name = "profile_id", referencedColumnName = "profile_id",    nullable = false) })
private Profile profile;

@ManyToOne
@JoinColumns({ @JoinColumn(name = "extension_id", referencedColumnName = "extension_id", nullable = false) })
private Extension extension;

@Column(name = "type")
private ExtensionType extensionType;

@Column(name = "name")
private ExtensionName extensionName;
}
4

1 回答 1

1

只有一列时,不应使用 JoinColumns。只需使用 JoinColumn:

@ManyToOne
@JoinColumn(name = "profile_id", nullable = false)
private Profile profile;

@ManyToOne
@JoinColumn(name = "extension_id", nullable = false)
private Extension extension;

现在另一个问题是 profile_id 和 extension_id 被映射为 ID 列和 ManyToOne 关联。这是可能的,但需要额外的配置。对于所有其他实体,您应该这样做,坚韧,并使用单列,自动生成的 ID,映射到 Long 类型的单个字段。事情会变得更简单,更高效。

此外,请考虑尊重 Java 命名约定,从而命名您的字段profileId而不是profile_id(当然,其他字段和 getter/setter 也是如此)。

于 2013-07-13T09:52:56.270 回答