首页 > 代码库 > 树状映射
树状映射
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讲义笔记
本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1879778
树状映射
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。