Hibernate中,基于Annotation的简单树形结构的实现

Hibernate中,基于Annotation的简单树形结构的实现

1)Node.java

01package com.hibernate.model;
02 
03import java.util.HashSet;
04import java.util.Set;
05 
06import javax.persistence.CascadeType;
07import javax.persistence.Entity;
08import javax.persistence.FetchType;
09import javax.persistence.GeneratedValue;
10import javax.persistence.Id;
11import javax.persistence.JoinColumn;
12import javax.persistence.ManyToOne;
13import javax.persistence.OneToMany;
14 
15@Entity
16public class Node {
17     
18    private int id;
19    private String name;
20    private Node parent;
21    private Set<Node> children = new HashSet<Node>();
22 
23    @Id
24    @GeneratedValue
25    public int getId() {
26        return id;
27    }
28    public void setId(int id) {
29        this.id = id;
30    }
31    public String getName() {
32        return name;
33    }
34    public void setName(String name) {
35        this.name = name;
36    }
37    @ManyToOne(cascade=CascadeType.ALL)
38    @JoinColumn(name="nodeId")
39    public Node getParent() {
40        return parent;
41    }
42    public void setParent(Node parent) {
43        this.parent = parent;
44    }
45    @OneToMany(mappedBy="parent", cascade=CascadeType.ALL,fetch=FetchType.EAGER)
46    public Set<Node> getChildren() {
47        return children;
48    }
49    public void setChildren(Set<Node> children) {
50        this.children = children;
51    }
52}

2)hibernate.cfg.xml
01<?xml version='1.0' encoding='UTF-8'?>
02<!DOCTYPE hibernate-configuration PUBLIC
03          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
04          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
05 
06<!-- Generated by MyEclipse Hibernate Tools.                   -->
07<hibernate-configuration>
08 
09    <session-factory>
10        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
11        <property name="connection.url">jdbc:mysql://localhost:3306/pos</property>
12        <property name="connection.username">root</property>
13        <property name="connection.password">123456</property>
14        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
15        <property name="myeclipse.connection.profile">Mysql Driver</property>
16         <!-- Echo all executed SQL to stdout -->
17        <property name="show_sql">true</property>
18        <property name="format_sql">true</property>
19 
20        <mapping class="com.hibernate.model.Node"/>
21 
22    </session-factory>
23 
24</hibernate-configuration>

3)log4j.properties

1log4j.rootLogger=info, stdout
2log4j.logger.org.hibernate.test=info
3log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4log4j.appender.stdout.Target=System.out
5log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
6log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
7log4j.logger.org.hibernate.tool.hbm2ddl=debug

4)HibernateTest.java

 

01package com.hibernate;
02 
03import org.hibernate.Session;
04import org.hibernate.SessionFactory;
05import org.hibernate.cfg.AnnotationConfiguration;
06import org.hibernate.tool.hbm2ddl.SchemaExport;
07import org.junit.AfterClass;
08import org.junit.BeforeClass;
09import org.junit.Test;
10 
11import com.hibernate.model.Node;
12 
13public class HibernateTest {
14     
15    private static SessionFactory sessionFactory;
16     
17    @BeforeClass
18    public static void beforeClass() {
19        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
20        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
21    }
22     
23    @AfterClass
24    public static void afterClass() {
25        sessionFactory.close();
26    }
27 
28    public void testSave() {
29        Session session = sessionFactory.openSession();
30        Node node0 = new Node();
31        node0.setName("父亲");
32        Node node1 = new Node();
33        node1.setName("大儿子");
34        Node node2 = new Node();
35        node2.setName("小儿子");
36        Node node3 = new Node();
37        node3.setName("大儿子的大儿子");
38        Node node4 = new Node();
39        node4.setName("大儿子的小儿子");
40        Node node5 = new Node();
41        node5.setName("小儿子的大儿子");
42        Node node6 = new Node();
43        node6.setName("小儿子的小儿子");
44         
45        node0.getChildren().add(node1);
46        node0.getChildren().add(node2);
47        node1.getChildren().add(node3);
48        node1.getChildren().add(node4);
49        node1.setParent(node0);
50        node2.getChildren().add(node5);
51        node2.getChildren().add(node6);
52        node2.setParent(node0);
53        node3.setParent(node1);
54        node4.setParent(node1);
55        node5.setParent(node2);
56        node6.setParent(node2);
57         
58        session.beginTransaction();
59        session.save(node0);
60        session.getTransaction().commit();
61        session.close();
62    }
63    @Test
64    public void testLoad() {
65        testSave();
66        Session session = sessionFactory.openSession();
67        session.beginTransaction();
68        Node node = (Node)session.load(Node.class, 1);
69        print(node,0);
70        session.getTransaction().commit();
71        session.close();
72    }
73 
74    private void print(Node node,int level) {
75        String preStr = "";
76        for(int i=0;i<level;i++){
77            preStr +="----";
78        }
79        System.out.println(preStr+node.getName());
80        for(Node children:node.getChildren()){
81            print(children,level+1);
82        }  
83    }
84}

 5)Console:

原文地址:http://my.oschina.net/huangcongmin12/blog/77577

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值