尝试持久化多对一实体时出现错误:
内部异常:org.postgresql.util.PSQLException:错误:在表“concept”上插入或更新违反了外键约束“concept_concept_class_fk”详细信息:表“concept_class”中不存在键(concept_class_id)=(Concept)。错误代码:0 调用:INSERT INTO concept (concept_key, description, label, code, concept_class_id) VALUES (?, ?, ?, ?, ?) bind => [27, description_1, label_1, code_1, Concept] 查询:InsertObjectQuery( com.mirth.results.entities.Concept[conceptKey=27]) 在 com.sun.ejb.containers.BaseContainer.checkExceptionClientTx(BaseContainer.java:3728) 在 com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java :3576) 在 com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1354) ... 101 更多
这是尝试持久化它的方法。我在该行的位置发表了评论:
@Override
public void loadConcept(String metaDataFilePath, String dataFilePath) throws Exception {
try {
ConceptClassMetaData conceptClassMetaData = (ConceptClassMetaData) ModelSerializer.getInstance().fromXML(FileUtils.readFileToString(new File(metaDataFilePath), "UTF8"));
em.executeNativeQuery(conceptClassMetaData.getCreateStatement());
ConceptClassRow conceptClassRow = conceptClassMetaData.getConceptClassRow();
ConceptClass conceptClass = em.findByPrimaryKey(ConceptClass.class, conceptClassRow.getId());
if (conceptClass == null) {
conceptClass = new ConceptClass(conceptClassRow.getId());
}
conceptClass.setLabel(conceptClassRow.getLabel());
conceptClass.setOid(conceptClassRow.getOid());
conceptClass.setDescription(conceptClassRow.getDescription());
conceptClass = em.merge(conceptClass);
DataParser dataParser = new DataParser(conceptClassMetaData, dataFilePath);
for (ConceptModel conceptModel : dataParser.getConceptRows()) {
ConceptFilter<Concept> filter = new ConceptFilter<Concept>(Concept.class);
filter.setCode(conceptModel.getCode());
filter.setConceptClass(conceptClass.getLabel());
List<Concept> concepts = em.findAllByFilter(filter);
Concept concept = new Concept();
if (concepts != null && !concepts.isEmpty()) {
concept = concepts.get(0);
}
concept.setCode(conceptModel.getCode());
concept.setDescription(conceptModel.getDescription());
concept.setLabel(conceptModel.getLabel());
concept.setConceptClass(conceptClass);
concept = em.merge(concept); //THIS LINE CAUSES THE ERROR!
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
...
以下是这两个实体的定义方式:
@Entity
@Table(name = "concept")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="concept_class_id", discriminatorType=DiscriminatorType.STRING)
public class Concept extends KanaEntity {
@Id
@Basic(optional = false)
@Column(name = "concept_key")
protected Integer conceptKey;
@Basic(optional = false)
@Column(name = "code")
private String code;
@Basic(optional = false)
@Column(name = "label")
private String label;
@Column(name = "description")
private String description;
@JoinColumn(name = "concept_class_id", referencedColumnName = "id")
@ManyToOne
private ConceptClass conceptClass;
...
@Entity
@Table(name = "concept_class")
public class ConceptClass extends KanaEntity {
@Id
@Basic(optional = false)
@Column(name = "id")
private String id;
@Basic(optional = false)
@Column(name = "label")
private String label;
@Column(name = "oid")
private String oid;
@Column(name = "description")
private String description;
....
而且,重要的是正在生成的 sql:
INSERT INTO concept_class (id, oid, description, label) VALUES (?, ?, ?, ?) bind => [ LOINC_TEST , 2.16.212.31.231.54, 这是 LOINC_TEST 的元数据文件, loinc_test]
插入概念(概念键,描述,标签,代码,概念类ID)值(?,?,?,?,?)绑定=> [27,描述_1,标签_1,代码_1,概念]
失败的原因很明显:它为concept_class_id插入了Concept这个词。它应该插入单词LOINC_TEST。我想不通它为什么用这个词。我使用调试器查看了 Concept 和 ConceptClass 实例,它们都没有包含这个词。我正在使用eclipselink。有谁知道为什么会这样?