请设计一个树型结构,完成下列需求:
1、 任意一个节点只能有一个(或0个)父节点
2、 任意一个节点可以包含多个子节点
3、 给定任意一个节点,可以输出这个节点的父节点,以及父节点的父节点,一直到顶级节点,要求输出的时候,从顶级节点开始一直输出到给定的节点为止
要求:给出实体类代码、hibernate映射文件代码以及测试代码:
实体类:树节点:Node.java
package com.bjsxt.hibernate;
import java.util.Set;
public class Node {
private int id;
private String name;
private Node parent;
private Set childrens; 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 Node getParent() {
return parent;
} public void setParent(Node parent) {
this.parent = parent;
} public Set getChildrens() {
return childrens;
} public void setChildrens(Set childrens) {
this.childrens = childrens;
}
}
hibernate配置文件:Node.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Node" table="t_node">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/><!--下面为父亲节点的映射-->
<many-to-one name="parent" column="parentid" cascade="all" /><!--下面为孩子节点的映射--->
<set name="childrens" cascade="all" table="t_node" order-by="id">
<key column="parentid"></key>
<one-to-many class="com.bjsxt.hibernate.Node" />
</set>
</class>
</hibernate-mapping>
测试NodeTest.java:
package com.bjsxt.hibernate;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;import junit.framework.TestCase;
import org.hibernate.Session;
/**
* 测试hibernate建立的普通树
* @author Administrator
*
*/
public class SessionTest extends TestCase {
/**
* 向数据库中存入数据
*/
public void testSave1(){
Session session = null;
try{
session = HibernateUtils.getSession();
session.beginTransaction();
Node node1 = new Node();
node1.setName("根节点");
Node node2 = new Node();
node2.setName("节点2");
node2.setParent(node1);
session.save(node2);
Node node3 = new Node();
node3.setName("节点3");
node3.setParent(node1);
session.save(node3);
Node node4 = new Node();
node4.setName("节点4");
node4.setParent(node1);
session.save(node4);
Node node5 = new Node();
node5.setName("节点5");
node5.setParent(node2);
session.save(node5);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
/**
* 给定任意一个节点,可以输出这个节点的父节点,以及父节点的父节点,
* 一直到顶级节点,要求输出的时候,
* 从顶级节点开始一直输出到给定的节点为止
*/
public void testQuery(){
Session session = null;
try{
session = HibernateUtils.getSession();
session.beginTransaction();
Node node = (Node)session.load(Node.class, 5);
Node parent = node.getParent();
Set parents = new HashSet();
while(parent != null){
parents.add(parent);
parent = parent.getParent();
}
for(Iterator iter = parents.iterator();iter.hasNext();){
Node p1 = (Node)iter.next();
System.out.println("parent:" + p1.getName());
}
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
}
其中用到的包装类: HibernateUtils.java
package com.bjsxt.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class HibernateUtils {
private static SessionFactory factory;
public HibernateUtils(){
}
static{
try{
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getSession(){
return factory.openSession();
}
public static void closeSession(Session session){
if(session != null){
if(session.isOpen()){
session.close();
}
}
}
}
导出数据库的工具类:ExportToDB.java
package com.bjsxt.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportToDB {
public static void main(String[] aegs)throws Exception{
//读取配置文件
Configuration cfg = new Configuration().configure();
//创建SchemaExport对象
SchemaExport export = new SchemaExport(cfg);
//创建数据库
export.create(true, true);
}
}
标签:Node,hibernate,public,普通,session,一棵,import,节点
From: https://blog.51cto.com/u_2544485/7396885