gpt4 book ai didi

java - 保存对其他实体的引用

转载 作者:行者123 更新时间:2023-11-30 22:58:23 25 4
gpt4 key购买 nike

在一个在 MySQL 数据库上使用 hibernate 的 spring mvc 应用程序中,我有一个 Document 实体,每个 document 对象包含不同类型的 codes它们都是 Code 实体的实例。 Code 实体包含一个不同的可能值列表,这些值由 code.pk.codecode.pk.codesystem 的组合定义, pk 是对复合主键的引用。每个 code 对象可能被许多 document 对象引用。

我如何在 hibernate 和 MySQL 中映射它?

我现在安排事情的方式,每次我尝试存储包含任何 code 已经存储在底层 MySQL 数据库的 codes 表中。具体来说,当我的 dao 运行以下代码行时:

this.entitymanager.persist(document)  

立即抛出以下错误:

Hibernate: insert into codes (displayname, code, codesystem) values (?, ?, ?)
WARN SqlExceptionHelper - SQL Error: 1062, SQLState: 23000
ERROR SqlExceptionHelper - Duplicate entry 'somecode-somecodesystem' for key 'PRIMARY'

程序在那一点崩溃了,所以我不知道接下来是否会将 document 保存到数据库中的 documents 表中。为什么 它会尝试保存到 codes 表?有没有什么方法可以设置,以便它只将对 code 的引用保存到 documents 表中? 删除关系并将键作为字符串存储在 documents 表中,而不将 codes 连接到 document

这是我的java:

@Entity
@Table(name = "documents")
public class Document {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer id;

@ManyToOne(cascade = {CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinColumns({ @JoinColumn(name = "confidentiality_code", referencedColumnName = "code"),
@JoinColumn(name = "confidentiality_codesystem", referencedColumnName = "codesystem"),
})
public Code conftype;

//other stuff, then getters and setters
}

@Entity
@Table(name = "codes")
public class Code implements Serializable{

private static final long serialVersionUID = 1L;

@EmbeddedId
public EmbedCodePK codePk;

@Column(name="displayname")
private String displayname;

public EmbedCodePK getCodePk(){return codePk;}
public void setCodePk(EmbedCodePK mypk){codePk = mypk;}

public String getDisplayname(){return displayname;}
public void setDisplayname(String dspnm){displayname=dspnm;}

}

@Embeddable
public class EmbedCodePK implements Serializable {

private static final long serialVersionUID = 652312726451130179L;

@Column(name="code", nullable=false)
public String code;

@Column(name="codesystem", nullable=false)
private String codesystem;

public EmbedCodePK() {}
public EmbedCodePK(String c, String cs) {
this.code = c;
this.codesystem = cs;
}

/** getters and setters **/
public String getCode(){return code;}
public void setCode(String c){code=c;}

public void setCodesystem(String cs) {this.codesystem = cs;}
public String getCodesystem() {return codesystem;}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final EmbedCodePK other = (EmbedCodePK) obj;
if (code == null) {
if (other.code != null) return false;
} else if (!code.equals(other.code)) return false;
if (codesystem == null) {
if (other.codesystem != null) return false;
} else if (!codesystem.equals(other.codesystem)) return false;
return true;
}

@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + ((code == null) ? 0 : code.hashCode());
hash = 53 * hash + ((codesystem == null) ? 0 : codesystem.hashCode());
return hash;
}
}

最佳答案

解决方案是从 ManyToOne 关系中删除 CascadeType.ALL,这样它现在看起来如下:

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumns({ @JoinColumn(name = "confidentiality_code", referencedColumnName = "code"),
@JoinColumn(name = "confidentiality_codesystem", referencedColumnName = "codesystem"),
})
public Code conftype;

此问题现已成功解决。问题已回答。

关于java - 保存对其他实体的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25208324/

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