我正在使用hibernate 4.3.8。在Java Spring Web应用程序和postgres db中。
我有记录A和两个外键来记录B和C.
记录B已存在于系统中。记录C是新的,将在保存记录A时添加。记录布局就是这样。
A.primaryKey
A.foreignKey to B.primaryKey (already exists in system)
A.foreignKey to C.primaryKey (new record)
A.feildX
如何保存记录A?
谢谢你的帮助
这是实体类。我是hibernate的新手,它解释了错误。
@Entity
@Table(name="atable")
public class Aclass {
@Id
@Column(name="arec_id")
private String id;
//Do not create a reference to the bclass in this object. however do write the bclass object with a foreign key reference back to this aclass object
? @Transient
? @OneToOne(cascade=?)
? @JoinTable(name="btable",inverseJoinColumns=@JoinColumn(name="brec_id"))
? private Bclass bclass;
//Create a reference to the cclass object in this record and write the cclass object as well
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="crec_foreign_key",referencedColumnName="crec_id")
private Cclass cclass;
@Column(name="description")
private String description;
private VoiceEngineModel voiceEngineModel;
}
@Entity
@Table(name="btable")
public class Bclass {
@Id
@Column(name="brec_id")
private String id;
@Column(name="aref")
private String aref;
@Column(name="description")
private String description;
}
@Entity
@Table(name="ctable")
public class Cclass {
@Id
@Column(name="crec_id")
private String id;
@Column(name="description")
private String description;
}
我找到了解决方案。很容易。 Bclass的原始代码是错误的。抱歉。