- package com.bjsxt.hibernate;
- import java.util.Set;
- public class Node {
- // 标识符
- private int id;
- // 节点名称
- private String name;
- // 层次
- private int level;
- // 是否叶子节点
- private boolean leaf;
- // 父节点 * --- 1
- private Node parent;
- // 子节点 1 --- *
- private Set children;
- public Set getChildren() {
- return children;
- }
- public void setChildren(Set children) {
- this.children = children;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public boolean isLeaf() {
- return leaf;
- }
- public void setLeaf(boolean leaf) {
- this.leaf = leaf;
- }
- public int getLevel() {
- return level;
- }
- public void setLevel(int level) {
- this.level = level;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Node getParent() {
- return parent;
- }
- public void setParent(Node parent) {
- this.parent = parent;
- }
- }
- <?xml version="1.0" ?>
- <!DOCTYPE hibernate-mapping (View Source for full doctype...)>
- - <hibernate-mapping default-cascade="none" default-access="property" default-lazy="true" auto-import="true">
- - <class name="com.bjsxt.hibernate.Node" table="t_node" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version">
- - <id name="id">
- <generator class="native" />
- </id>
- <property name="name" unique="false" optimistic-lock="true" lazy="false" generated="never" />
- <property name="level" unique="false" optimistic-lock="true" lazy="false" generated="never" />
- <property name="leaf" unique="false" optimistic-lock="true" lazy="false" generated="never" />
- <many-to-one name="parent" column="pid" unique="false" update="true" insert="true" optimistic-lock="true" not-found="exception" embed-xml="true" />
- - <set name="children" lazy="extra" inverse="true" sort="unsorted" mutable="true" optimistic-lock="true" embed-xml="true">
- <key column="pid" on-delete="noaction" />
- <one-to-many class="com.bjsxt.hibernate.Node" not-found="exception" embed-xml="true" />
- </set>
- </class>
- </hibernate-mapping>
- package com.bjsxt.hibernate;
- import java.io.File;
- import java.util.Iterator;
- import java.util.Set;
- import org.hibernate.Session;
- public class NodeManager {
- private static NodeManager nodeManager;
- private NodeManager() {
- }
- public static synchronized NodeManager getInstance() {
- if (nodeManager == null) {
- nodeManager = new NodeManager();
- }
- return nodeManager;
- }
- // 创建树型结构
- public void createTree(String dir) {
- Session session = HibernateUtils.getSession();
- try {
- session.beginTransaction();
- File root = new File(dir);
- saveTree(root, session, null, 0);
- session.getTransaction().commit();
- } catch (RuntimeException e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- } finally {
- HibernateUtils.closeSession(session);
- }
- }
- // 递归创建一棵树
- private void saveTree(File file, Session session, Node parent, int level) {
- if (file == null || !file.exists()) {
- return;
- }
- boolean isLeaf = file.isFile();
- Node node = new Node();
- node.setName(file.getName());
- node.setLevel(level);
- node.setParent(parent);
- node.setLeaf(isLeaf);
- session.save(node);
- File[] subs = file.listFiles();
- if (subs != null && subs.length > 0) {
- for (int i = 0; i < subs.length; i++) {
- saveTree(subs[i], session, node, level + 1);
- }
- }
- }
- public void printTree(int id) {
- Session session = HibernateUtils.getSession();
- try {
- session.beginTransaction();
- Node root = (Node) session.load(Node.class, id);
- printNode(root);
- session.getTransaction().commit();
- } catch (RuntimeException e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- } finally {
- HibernateUtils.closeSession(session);
- }
- }
- private void printNode(Node node) {
- if (node == null) {
- return;
- }
- int level = node.getLevel();
- if (level > 0) {
- for (int i = 0; i < level; i++) {
- System.out.print(" |");
- }
- System.out.print("--");
- }
- System.out.println(node.getName()
- + (node.isLeaf() ? "" : "[" + node.getChildren().size() + "]"));
- Set children = node.getChildren();
- for (Iterator iter = children.iterator(); iter.hasNext();) {
- Node child = (Node) iter.next();
- printNode(child);
- }
- }
- }