hibernate —— 树状存储

 1 package com.pt.treeStrut;
 2 
 3 import java.util.Set;
 4 
 5 import javax.persistence.CascadeType;
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.Id;
 9 import javax.persistence.JoinColumn;
10 import javax.persistence.ManyToOne;
11 import javax.persistence.OneToMany;
12 
13 @Entity
14 public class ArticleTree {
15     int id;
16     String title;
17     String content;
18     ArticleTree p_article;
19     
20     Set<ArticleTree> sons;
21     
22     @OneToMany(mappedBy="p_article",cascade=CascadeType.ALL)
23     public Set<ArticleTree> getSons() {
24         return sons;
25     }
26     
27     
28     public void setSons(Set<ArticleTree> sons) {
29         this.sons = sons;
30     }
31     public ArticleTree(){
32         
33     }
34     public ArticleTree(int id, String title, String content){
35         setId(id);
36         setTitle(title);
37         setContent(content);
38     }
39     @Id
40     //@GeneratedValue
41     public int getId() {
42         return id;
43     }
44     public void setId(int id) {
45         this.id = id;
46     }
47     public String getTitle() {
48         return title;
49     }
50     public void setTitle(String title) {
51         this.title = title;
52     }
53     public String getContent() {
54         return content;
55     }
56     public void setContent(String content) {
57         this.content = content;
58     }
59     
60     @ManyToOne(cascade=CascadeType.ALL)
61     @JoinColumn(name="parent_id")
62     public ArticleTree getP_article() {
63         return p_article;
64     }
65     public void setP_article(ArticleTree p_article) {
66         this.p_article = p_article;
67     }
68 
69 
70     @Override
71     public String toString() {
72         // TODO Auto-generated method stub
73         return title + "  " + content;
74     }
75     
76     
77 }
ArticleTree.java
 1 import java.sql.Connection;
 2 import java.util.HashSet;
 3 import java.util.Set;
 4 
 5 import org.hibernate.Session;
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.cfg.Configuration;
 8 
 9 import com.pt.hibernate.Student;
10 import com.pt.hibernate.Teacher;
11 import com.pt.treeStrut.ArticleTree;
12 
13 
14 public class Test {
15     public static void main(String[] arges){
16         Configuration cfg = new Configuration();
17         SessionFactory factory = cfg.configure().buildSessionFactory();
18         //Session ss=factory.openSession();
19         Session ss= factory.getCurrentSession();
20 //        Teacher hong = new Teacher();
21 //        hong.setName("gaolihong");
22 //        hong.setCourese("english");
23 //        
24 //        Set<Student> students=new HashSet<Student>();
25 //        Student student = new Student();
26 //        student.setId(2);
27 //        student.setSchoolName("DuYangGang2");
28 //        students.add(student);
29 //        hong.setMyStudents(students);
30         ss.beginTransaction();
31 //        ss.save(hong);
32 //        ss.getTransaction().commit();
33         ArticleTree a_p = new ArticleTree(1000,"第一章","导论");
34         ArticleTree a_s1 = new ArticleTree(1001,"第一节","导论——1");
35         ArticleTree a_s2 = new ArticleTree(1002,"第二节","导论——2");
36         
37         ArticleTree a_p1 = new ArticleTree(2000,"第二章","光学");
38         ArticleTree a_s11 = new ArticleTree(2001,"第一节","光学——1");
39         ArticleTree a_s12 = new ArticleTree(2002,"第二节","光学——2");
40         
41         a_s11.setP_article(a_p1);
42         a_s12.setP_article(a_p1);
43         a_p1.setSons(new HashSet<ArticleTree>());    //否则会报空指针异常
44         a_p1.getSons().add(a_s11);
45         a_p1.getSons().add(a_s12);
46         
47         
48         a_s1.setP_article(a_p);
49         a_s2.setP_article(a_p);
50         a_p.setSons(new HashSet<ArticleTree>());
51         a_p.getSons().add(a_s1);
52         a_p.getSons().add(a_s2);
53         
54         ArticleTree all = new ArticleTree(1,"物理","物理");
55         a_p1.setP_article(all);
56         a_p.setP_article(all);
57         all.setSons(new HashSet<ArticleTree>());    //否则会报空指针异常
58         all.getSons().add(a_p);
59         all.getSons().add(a_p1);
60         
61         //ss.save(all);    //由于设置了cascade,存储一个,整个树都会存储进去
62         
63         //ss.save(a_s1);
64         ss.save(a_s1);    //由于设置了cascade,存储一个,整个树都会存储进去
65         ss.getTransaction().commit();
66         //ss.close();        //getsession  不用close
67         
68         /****取出树来,并打印*******/
69         Session ss2= factory.getCurrentSession();
70         ss2.beginTransaction();
71         ArticleTree getAll = ss2.load(ArticleTree.class, 1);  //取出父节点
72         printTree(getAll,0);
73         ss2.getTransaction().commit();
74         factory.close();
75     }
76     
77     public static void printTree(ArticleTree parent,int level){
78         for(int i = 0; i< level; i++){
79             System.out.print("--");
80         }
81         System.out.println(parent);
82         for(ArticleTree article : parent.getSons()){
83             printTree(article, level + 1);
84         }
85     }
86 }
test

 

PS:开发过程中写的bug:

  1、commit的时候提示没有session   原因:session没有beginTransion

  2、提示set<sons>  字段找不到匹配的数据库字段类型,  原因:@oneTomany 写在了get上

posted @ 2016-06-14 21:08  沙中世界  阅读(202)  评论(0编辑  收藏  举报