Lets say I have bidirectional one-to-many association between Parent-Child, mapped as follows:
Parent.java:
@Entity
public class Parent {
@Id
private Integer id;
@OneToMany(mappedBy = "parent")
private List<Child> childs = new ArrayList<>();
...
and Child.java:
@Entity
public class Child {
@Id
private Integer id;
@ManyToOne
@JoinColumn(name = "parent_id")
private Parent parent;
...
When I run this code
Parent parent = new Parent(1);
Child child = new Child(1);
Child child2 = new Child(2);
child.setParent(parent);
child2.setParent(parent);
parent.getChilds().add(child);
parent.getChilds().add(child2);
parentRepository.save(parent);
I get exception
Unable to find Child with id 1
Saving a child first doesn't help either, only exception is different