十七、树状结构的设计

在同—个类中使用One2Many和Many2One

@Entity
public class Org {
    private int id;
    private String name;
    private Set children = new HashSet();
    private Org parent;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
    public Set getChildren() {
        return children;
    }
    public void setChildren(Set children) {
        this.children = children;
    }
   
    @ManyToOne
    @JoinColumn(name="parent_id")
    public Org getParent() {
        return parent;
    }
    public void setParent(Org parent) {
        this.parent = parent;
    }
}

你可能感兴趣的:(hibernate3.3.2)