I have ManyToOne relationship between Parent and Child. I want to Delete multiple Child entities that answer a certain query. The problem is that after I run a Delete query, Parent.getChildren() still returns the deleted children.
Can't I use Delete queries in such case?
@Entity
@Table(name = "CHILD_DATA")
public class Child {
private Parent parent;
}
@Entity
@Table(name = "PARENT")
public class Parent{
private Set<Child> children;
@Column(name = "CHILDREN")
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public Set<Child> getChildren() {
return children;
}
}
public class ChildDAO{
public int removeServiceFrontPageData(Parent parent, long serviceID){
String query = "DELETE FROM Child WHERE parent =:parent";
Query q = em.createQuery(query);
q.setParameter("parent", parent);
return q.executeUpdate();
}
}
To refresh a parent entity, I use the following function:
public class ParentDAO{
public Parent getParent(String parentID){
final String select = "FROM Parent WHERE parentID = :parentID";
Query q = em.createQuery(select);
q.setParameter("parentID", parentID);
if(q.getResultList().isEmpty()){
return null;
}
return (Parent) q.getSingleResult();
}
}
thanks