Node类:
package com.bjsxt.hibernate.树状映射;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
/** * 节点*/
@Entity
public class Node {
private int id; //标识符
private String name; //节点名称
private int level; //层次,为了输出设计
private boolean leaf; //是否为叶子节点,这是为了效率设计,可有可无
//父节点:因为多个节点属于一个父节点,因此用hibernate映射关系说是“多对一”
private Node parent;
//子节点:因为一个节点有多个子节点,因此用hibernate映射关系说是“一对多”
private Set<Node> children;
@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;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
@ManyToOne
@JoinColumn(name="pid")
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
@OneToMany(mappedBy="parent")
public Set<Node> getChildren() {
return children;
}
public Node(int id, String name, int level, boolean leaf, Node parent,
Set<Node> children) {
super();
this.id = id;
this.name = name;
this.level = level;
this.leaf = leaf;
this.parent = parent;
this.children = children;
}
public Node() {
super();
// TODO Auto-generated constructor stub
}
public void setChildren(Set<Node> children) {
this.children = children;
}
}
笔记推荐:马士兵hibenate讲义笔记
转载于:https://blog.51cto.com/matengbing/1879778