次のようなテーブル構造があります。
Table_A(
a_id,
data,
primary key(a_id)
)
Table_B(
b_id,
a_id,
data,
primary key (b_id),
foreign key (a_id) references Table_A(a_id)
)
Table_A と Table_B の間には 1 対多の関係があります。私の質問は、これらのテーブルごとにエンティティがあるかどうかです。
- エンティティ Table_A は、Table_B エンティティのリストと
- エンティティ Table_B は必ずしも Table_A への参照を必要としない (フィールド b_id、a_id、data のみ)
Table_Aエンティティを永続化できる方法でこれらのエンティティに注釈を付けることは可能ですか?プロセスは、新しく生成されたTable_A主キーa_idの値を持つすべてのTable_Bエンティティを暗黙的に永続化します.
前もって感謝します。
これが私が本質的に持っているものです。しかし、私は以下の例外を受け取ります。Table_A の前に Table_B が永続化されているように見えるため、a_id 値が存在しません。
@Entity
public class Table_A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "a_id")
private Integer id;
@OneToMany (cascade = CascadeType.PERSIST)
@JoinColumn(name="a_id")
private List<Table_B> bList;
private String data;
}
@Entity
public class Table_B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "b_id")
private Integer id;
private String data;
}
エラー: 列 "a_id" の null 値が not-null 制約に違反しています 詳細: 失敗した行には (null, Testing, 16) が含まれています