users@glassfish.java.net

Re: JPA - ManyToMany deletes to much

From: <glassfish_at_javadesktop.org>
Date: Thu, 22 Nov 2007 04:09:23 PST

i have a temporary fix that first checks all the childs (recursively)

    public void deleteNodesWithMainId(@WebParam(name = "main_id") Integer mainId) {
        deleteNode(em.find(Node.class, mainId));
    }
    
    private void deleteNode(Node n) {
        Iterator<Node> it = n.getChildNodes().iterator();
        while(it.hasNext()) {
            Node nChild = it.next();
            if(nChild.getParentNodes().size() == 1)
                deleteNode(nChild); //delete recursively. this doesn't result into a livelock as loops aren't allowed in the graph
            else {
                it.remove();
                nChild.getParentNodes().remove(n);
            }
        }
        em.remove(n);
    }

but now i'm not taking advantage of the JPA cascading effect while that is one of the prime reasons that i'm using it ;)

so if everyone knows how to do it with JPA cascades... please help me
[Message sent by forum member 'johnnyvdlaar' (johnnyvdlaar)]

http://forums.java.net/jive/thread.jspa?messageID=246767