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);
}
I suspect the dozerMapper does something with the collection. Try copying the fields from the DTO to the entity without using dozer.
Moreover, saveOrUpdate takes a detached instance, and attaches it to the session. It throws an exception if the attached version of the detached entity is already loaded in the session.
The parent passed to
saveOrUpdateis already attached, and the call tosaveOrUpdateis thus completely unnecessary (and could cause an exception). Changes made to an attached entity are automatically persisted by Hibernate. You don’t need to callsaveOrUpdate,updateormerge.