【问题标题】:Hibernate Composite Key Using Annotation , Having One Foreign Key使用注释的休眠复合键,具有一个外键
【发布时间】:2013-08-25 05:39:42
【问题描述】:

我用谷歌搜索了它,但找不到任何解决方案

I 两个类 Application.java 和 ApplicationVersion.java 他们有一对多的关系, ApplicationVersionPk.java 是一个可嵌入类,用作 ApplicationVersionPk.java 中的复合键 下面是我的类,当我运行我的 Web 应用程序时,我得到 Mapping Exception with

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

}

【问题讨论】:

    标签: hibernate one-to-many composite-key hibernate-annotations mappingexception


    【解决方案1】:

    您正在使用ApplicationVersion 旁边的两个键加入Application.appVersions,而不是唯一应用程序的@Id;你可以使用mappedBy="applicationVersionPk.application"

    【讨论】:

    • 感谢您的回复,为什么我使用 mappedBy="applicationVersionPk.version" 版本不是外键它是 ApplicationVersion 的列
    • 我使用了 applicationVersionPk.application,它使用了我的问题
    猜你喜欢
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2023-04-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多