2017-12-18 3 views
-1

私はhibernate 4.3.8を使用しています。 Java Spring Webアプリケーションとpostgres db既存のレコードへの外部キーを持つhibernateエンティティを作成する

IはBおよびC

レコードBがすでにシステムに存在し記録するために2つの外部キーを有するレコードAを有しています。 レコード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. 列が

    @Entity @Table(名= "BTABLE") パブリッククラスBclass {

    @Idを参照変更BclassためAClassは

  2. でBclassへの参照を削除 @Column(name = "brec_id") プライベート文字列ID。

    @ManyToOne @JoinColumn(name = "aref") private Aclass aref;

    @Column(name = "description") プライベートストリングの説明。

    }

関連する問題