使用JPA保留图表

时间:2012-08-21 13:05:57

标签: jpa graph persist

我尝试使用JPA保留图表,但它不起作用。我有一个Node(s),它可以有多个Parent(s),Parent可以有多个子节点(Node)。

@Entity
@Table(name = "NODES")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "KIND", discriminatorType = DiscriminatorType.STRING, length = 1)
public abstract class Node implements Serializable {

    @Column(unique = true, name = "NAME", nullable = false, length = 50)
    private String name;

    @ManyToMany(mappedBy = "children", fetch = FetchType.LAZY)
    @JoinTable(name = "NODE_PARENTS", joinColumns = { @JoinColumn(name = "NODEID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "PARENTID", referencedColumnName = "ID") })
    private Set<Parent> parents;

    public Node() {
        this.parents = new HashSet<Parent>();
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setParents(Collection<Parent> parentList) {
        for(Parent parent : this.parents) {
            removeParent(parent);
        }
        for (Parent parent : parentList) {
            addParent(parent);
        }
    }

    public void addParent(Parent parent) {
        this.parents.add(parent);
        if (!parent.getMembers().contains(this)) {
            parent.getMembers().add(this);
        }
    }

    public void removeParent(ACLGroup parent) {
        this.parents.remove(parent);
        if (parent.getMembers().contains(this)) {
            parent.getMembers().remove(this);
        }
    }

    public Set<ACLGroup> getParents() {
        return Collections.unmodifiableSet(this.parents);
    }
}

@Entity
@Table(name = "LEAFS")
@DiscriminatorValue("L")
public class Leaf extends Node {

    // some fields to persist
}

@Entity
@Table(name = "INNER_NODES")
@DiscriminatorValue("P")
public class InnerNodes extends Node implements Parent {

    // some fields to persist

    @ManyToMany(fetch = FetchType.LAZY)
    private Set<Node> children;

    public InnerNodes() {
        this.children = new HashSet<Node>();
    }

    public Set<Node> getChildren() {
        return Collections.unmodifiableSet(this.children);
    }

    public void setChildren(Set<Node> childList) {
        for (Node child : this.children) {
            removeChild(child);
        }
        for (Node child : childList) {
            addChild(child);
        }
    }

    public void addChild(Node child) {
        this.children.add(child);
        if (!child.getParents().contains(this)) {
            child.addParent(this);
        }
    }

    public void removeChild(Node child) {
        this.children.remove(child);
        if (child.getParents().contains(this)) {
            child.removeParent(this);
        }
    }
}

public interface Parent {
    Set<Node> getChildren();
    void setChildren(Set<Node> childList);
    void addChild(Node child);
    void removeChild(Node child);
}

问题是,我不能坚持接口Parent,但我需要它来阻止圈子。每个Leaf或InnerNode可以有多个Parent,每个Parent可以有多个孩子。

有任何想法实现吗? 非常感谢。

安德烈

0 个答案:

没有答案