@Entity
@Table(name = "parent")
@NoArgsConstructor
@AllArgsConstructor
@SequenceGenerator(name = "parentSequence", sequenceName = "parent_id_seq",
allocationSize = 1)
public class ParentDto implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "parentSequence")
private Long id;
private String name;
@OneToMany(mappedBy = "parent")
private List<Child> children;
}
@Entity
@AllArgsConstructor
@SequenceGenerator(name="childSequence", sequenceName = "child_id_seq", allocationSize=1)
@Table(name = "child")
public class Child{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "childSequence")
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "parent_id", nullable = false)
private Parent parent;
}
I have the next request to save mapped to property request (ids should be generated by DB)
{
"parent": {
"name": "Bob",
"children": [{
"name": "Bob2"
}]
}
}
Executing parentDao.save(request);
and trying to get child executing childDao.findAll(); which returns empty array.
When executing parentDao.findAll() - receiving parent which contains child object with id=null and parent=null.
Also while performing parentDao.save(request) with updated data getting such exception
Unable to find ChildDto with id 1; nested exception is javax.persistence.EntityNotFoundException: Unable to find ChildDto with id 1
If I update ParentDto with
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Child> children;
receive such exception
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: ChildDto