Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I try to insert using this relationship with cascade capability. The system got error:

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

A entity.

@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 entity.

@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 entity.

@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;

Execute.

    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);
like image 943
prptn Avatar asked Sep 18 '25 21:09

prptn


1 Answers

Your c does not know its b "parent" - try adding c.setB(b) before saving.

like image 126
Smutje Avatar answered Sep 20 '25 11:09

Smutje