两个蝴蝶飞 2010-03-16
请设计一个树型结构,完成下列需求:
1、任意一个节点只能有一个(或0个)父节点
2、任意一个节点可以包含多个子节点
3、给定任意一个节点,可以输出这个节点的父节点,以及父节点的父节点,一直到顶级节点,要求输出的时候,从顶级节点开始一直输出到给定的节点为止
要求:给出实体类代码、hibernate映射文件代码以及测试代码:
实体类:树节点:Node.java
packagecom.bjsxt.hibernate;
importjava.util.Set;
publicclassNode{
privateintid;
privateStringname;
privateNodeparent;
privateSetchildrens;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicNodegetParent(){
returnparent;
}
publicvoidsetParent(Nodeparent){
this.parent=parent;
}
publicSetgetChildrens(){
returnchildrens;
}
publicvoidsetChildrens(Setchildrens){
this.childrens=childrens;
}
}
hibernate配置文件:Node.hbm.xml:
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC
"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<classname="com.bjsxt.hibernate.Node"table="t_node">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<!--下面为父亲节点的映射-->
<many-to-onename="parent"column="parentid"cascade="all"/>
<!--下面为孩子节点的映射--->
<setname="childrens"cascade="all"table="t_node"order-by="id">
<keycolumn="parentid"></key>
<one-to-manyclass="com.bjsxt.hibernate.Node"/>
</set>
</class>
</hibernate-mapping>
测试NodeTest.java:
packagecom.bjsxt.hibernate;
importjava.util.HashSet;
importjava.util.Iterator;
importjava.util.Set;
importjunit.framework.TestCase;
importorg.hibernate.Session;
/**
*测试hibernate建立的普通树
*@authorAdministrator
*
*/
publicclassSessionTestextendsTestCase{
/**
*向数据库中存入数据
*/
publicvoidtestSave1(){
Sessionsession=null;
try{
session=HibernateUtils.getSession();
session.beginTransaction();
Nodenode1=newNode();
node1.setName("根节点");
Nodenode2=newNode();
node2.setName("节点2");
node2.setParent(node1);
session.save(node2);
Nodenode3=newNode();
node3.setName("节点3");
node3.setParent(node1);
session.save(node3);
Nodenode4=newNode();
node4.setName("节点4");
node4.setParent(node1);
session.save(node4);
Nodenode5=newNode();
node5.setName("节点5");
node5.setParent(node2);
session.save(node5);
session.getTransaction().commit();
}catch(Exceptione){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
/**
*给定任意一个节点,可以输出这个节点的父节点,以及父节点的父节点,
*一直到顶级节点,要求输出的时候,
*从顶级节点开始一直输出到给定的节点为止
*/
publicvoidtestQuery(){
Sessionsession=null;
try{
session=HibernateUtils.getSession();
session.beginTransaction();
Nodenode=(Node)session.load(Node.class,5);
Nodeparent=node.getParent();
Setparents=newHashSet();
while(parent!=null){
parents.add(parent);
parent=parent.getParent();
}
for(Iteratoriter=parents.iterator();iter.hasNext();){
Nodep1=(Node)iter.next();
System.out.println("parent:"+p1.getName());
}
session.getTransaction().commit();
}catch(Exceptione){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
}
其中用到的包装类:HibernateUtils.java
packagecom.bjsxt.hibernate;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.cfg.Configuration;
publicclassHibernateUtils{
privatestaticSessionFactoryfactory;
publicHibernateUtils(){
}
static{
try{
Configurationcfg=newConfiguration().configure();
factory=cfg.buildSessionFactory();
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticSessionFactorygetSessionFactory(){
returnfactory;
}
publicstaticSessiongetSession(){
returnfactory.openSession();
}
publicstaticvoidcloseSession(Sessionsession){
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
}
导出数据库的工具类:ExportToDB.java
packagecom.bjsxt.hibernate;
importorg.hibernate.cfg.Configuration;
importorg.hibernate.tool.hbm2ddl.SchemaExport;
publicclassExportToDB{
publicstaticvoidmain(String[]aegs)throwsException{
//读取配置文件
Configurationcfg=newConfiguration().configure();
//创建SchemaExport对象
SchemaExportexport=newSchemaExport(cfg);
//创建数据库
export.create(true,true);
}
}