hibernate树形结构

请设计一个树型结构,完成下列需求:

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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值