Pergunta

I have the following two entities:

    public class Parent implements Serializable
      @OneToMany(mappedBy="m_owner", targetEntity = Child.class, orphanRemoval=true)
      List<Child> children;

and

    public class Child implements Serializable
      @ManyToOne(targetEntity = Parent.class)

When I delete the Parent, the children are deleted as expected but when I delete the child, the Parent is removed. I tried adding the cascade=CascadeType.DETACH to the child's annotation but that did not help. How do I make it so deleting the children has no impact on the parent?

Foi útil?

Solução

My problem was caused by the fact that I used a NamedQuery to delete the child.

@NamedQuery(name="Child.deleteById", query="Delete from Child child where child.m_id=:id")

This appears to circumvent the annotations I had set up and deleted the parent entity.

To solve this, I changed the delete service to simply use the EntityManager.remove() at which point all the annotations worked.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top