2017-12-18 25 views
-1

我正在使用休眠4.3.8。在Java Spring Web应用程序和postgres数据库中。用现有记录的外键创建休眠实体

我有两个外键记录中的记录B和C.

记录B已经存在于系统中。 记录C是新的,当我保存记录A时将被添加。记录A布局就是这样。

A.primaryKey 
A.foreignKey to B.primaryKey (already exists in system) 
A.foreignKey to C.primaryKey (new record) 
A.feildX 

如何保存记录A?

感谢您的帮助

这里是classes.I是新冬眠这也解释了错误的实体。

@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",[email protected](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; 


} 
+0

邮报你的实体类以及你的持久性代码的源代码,如果你有的话还没有 –

回答

0

我想出了解决方案。好简单。 Bclass的原始代码是错误的。抱歉。

  1. 除去任何参考在ACLASS
  2. 到Bclass为Bclass修改列引用

    @Entity @Table(名称= “BTABLE”) 公共类Bclass {

    @Id @Column(name =“brec_id”) private String id;

    @ManyToOne @JoinColumn(name =“aref”) private Aclass aref;

    @Column(name =“description”) private String description;

    }