I have a parent child relationship between 2 classes
Parent
@Entity
@Table(name = "PARENT")
public class Parent {
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "WAGES")
private BigDecimal wages;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID")
private List<Child> children;
// getters and setters
}
Child
@Entity
@Table(name = "CHILD")
public class Child{
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "PARENT_ID", nullable = false, insertable = false, updatable = false)
private Long parentId;
// getters and setters
}
In this particular case, there are no child records for the parent. When I update the parent , hibernate throws the following exception even though the parent does not contain any child records and I am not trying to update/add/remove any child records. The children collection is not accesses at all in code. I am using all JPA annotations. Hibernate version is 3.6.7.Final. Any pointers is appreciated.
org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:com.mycode.Child
//Service code
public Parent update(ParentDto dto) {
Parent parent = parentDao.findById(dto.getId());
// Using Dozer to map dto object to entity object
// http://dozer.sourceforge.net/
dozerMapper.map(dto, parent);
taxFormW2Dao.saveOrUpdate(parent);
return Parent;
}
// Dao code
public void saveOrUpdate(Parent parent){
// HibernateTempplate is injected through Spring
HibernateTemplate template = getHibernateTemplate();
getHibernateTemplate().saveOrUpdate(parent);
}