2013-07-13 50 views
2

我想實現與額外列的多對多關係,但gettig低於錯誤。 有人可以解釋爲什麼下面的錯誤發生和如何解決?@JoinColumn必須爲每個連接列使用@JoinColumns指定

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; 

} 

ProfileExtensionMapping:

@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; 
} 

回答

0

當你只有一個你不應該使用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列並作爲多對一關聯。這是可能的,但需要額外的配置。您應該像所有其他實體一樣執行操作,並且使用單個列(自動生成的ID)映射到Long類型的單個字段。事情會更簡單,更高效。

此外,請考慮尊重Java命名約定,並因此命名您的字段profileId而不是profile_id(當然,其他字段和獲取者/設置者也是如此)。

相關問題