简体   繁体   中英

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

您的c不知道其b “父”-尝试在保存之前添加c.setB(b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM