gpt4 book ai didi

使用注解的 Hibernate 复合键,具有一个外键

转载 作者:行者123 更新时间:2023-12-04 04:44:20 25 4
gpt4 key购买 nike

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

I 两个类 Application.java 和 ApplicationVersion.java 他们有一对多的关系,
ApplicationVersionPk.java 是一个 Embedable 类,在 中用作复合键ApplicationVersionPk.java 以下是我运行我的网络应用程序时的类(class),我得到了映射异常

 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]):

应用程序.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;
}

}

最佳答案

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

关于使用注解的 Hibernate 复合键,具有一个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18426096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com