2

我GOOGLE了,但便無法找到任何解決辦法使用Hibernate複合鍵註釋,有一個外鍵

我兩節課Application.java和ApplicationVersion.java他們有一個一對多的關係, ApplicationVersionPk的.java是其在用作複合鍵ApplicationVersionPk.java一個Embedable類下面我的類時,我運行我的網絡應用程序我得到映射的異常與

nested exception is org.hibernate.MappingException: Foreign key (FKA50BD18824C3AEF5:ApplicationVersion [application_id,version])) must have same number of columns as the referenced primary key (Application [id]): 

Application.java

@Entity 
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "nameSpace" })) 
public class Application implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private Long id; 
    private Set<ApplicationVersion> appVersions = new HashSet<ApplicationVersion>(
      5); 

    @Id 
    @GeneratedValue 
    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 


    @OneToMany(fetch = FetchType.LAZY, mappedBy = "applicationVersionPk") 

    @Cascade(value = { CascadeType.DELETE }) 
    public Set<ApplicationVersion> getAppVersions() { 
     return appVersions; 
    } 

    public void setAppVersions(Set<ApplicationVersion> appVersions) { 
     this.appVersions = appVersions; 
    } 

} 

ApplicationVersion.java

@Entity 
public class ApplicationVersion implements Serializable { 

    private static final long serialVersionUID = 1L; 
    private Long id; 

    private ApplicationVersionPk applicationVersionPk; 

     @EmbeddedId 
    public ApplicationVersionPk getApplicationVersionPk() { 
     return applicationVersionPk; 
    } 

    public void setApplicationVersionPk(
      ApplicationVersionPk applicationVersionPk) { 
     this.applicationVersionPk = applicationVersionPk; 
    } 


    @GeneratedValue 
    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

} 

ApplicationVersionPk.java

@Embeddable 
public class ApplicationVersionPk implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    private Application application; 
    private String version; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumns({ @JoinColumn(referencedColumnName = "id", nullable = false) }) 
    public Application getApplication() { 
     return application; 
    } 

    public void setApplication(Application application) { 
     this.application = application; 
    } 

    public String getVersion() { 
     return version; 
    } 

    public void setVersion(String version) { 
     this.version = version; 
    } 

    public boolean equals(Object other) { 
     if ((this == other)) 
      return true; 
     if ((other == null)) 
      return false; 
     if (!(other instanceof ApplicationVersionPk)) 
      return false; 
     ApplicationVersionPk versionPk = (ApplicationVersionPk) other; 

     return (this.getApplication().getNameSpace() 
       .equalsIgnoreCase(versionPk.getApplication().getNameSpace()) && this.version 
       .equalsIgnoreCase(versionPk.getVersion())); 
    } 

    public int hashCode() { 
     int result = 17; 

     result = result + this.version.hashCode(); 
     result += result + this.getApplication().getNameSpace().hashCode(); 
     return result; 
    } 

} 

回答

3

您對ApplicationVersion,而不是唯一的應用程序的@Id的側結合Application.appVersions使用兩個密鑰;您可以使用mappedBy="applicationVersionPk.application"

+0

感謝您的答覆,我爲什麼用的mappedBy =「applicationVersionPk.version」版本是不是外國的關鍵是ApplicationVersion –

+1

我用applicationVersionPk.application的列,並將其用於我的問題 –

+0

你說得對,我錯誤。編輯我的答案 –