One-To-Many Cascade Save, ERROR: null value in column "b_id" violates not-null constraint

One-To-Many Cascade Save, ERROR: null value in column "b_id" violates not-null constraint

当我尝试使用这种具有级联能力的关系进行插入时。系统出现错误:

ERROR: null value in column "b_id" violates not-null constraint

一个实体。

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "b_id")
private B b;

B实体。

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;

@OneToMany(mappedBy = "b", cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
private List<C> cs;

C实体。

@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "b_id", nullable = false, referencedColumnName = "id")
private B b;

执行。

    A a = new A();
    B b = new B();
    C c = new C();

    List<C> cs = new ArrayList<C>();
    cs.add(c);

    b.setCs(cs);
    a.setB(b);

    aRepository.save(a);

您的 c 不知道它的 b "parent" - 尝试在保存前添加 c.setB(b)