I would like to understand this basic concept. How do we save the child entity and populate the foreign Key ID in the Child table. I have this situation where Parent Can have many child. But the Child can only have one Parent.
`
Entity
@Table(name = "Child")
@SuppressWarnings("serial")
public class Child implements java.io.Serializable {
@ManyToOne
@JoinColumn(name = "parent_id")
private Parent parent;
}
-------------------------
@Entity
@Table(name = "parent")
@SuppressWarnings("serial")
public class Parent {
@OneToMany(mappedBy = "parent", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private List<Child> children;
}
` I am trying to save the child like this below. but its giving me exception that
column "parent" of relation "child" does not exist
Child child = new Child();
child.setParent(parent);
child.setXYZ("XYZ");
childRepository.save(child);
I am trying to save the child entity and expecting it to save the parent_id column automatically. I also tried creating another field in the child entity called
private Long parentId;
and tried to replace this line
child.setParent(parent)
with child.setParentId(parentId);
but nothing is working