2017-12-18 398 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;

    }