隨筆:15 文章:206 評論:35 引用:0
fenglin
創新、創意、挑戰
C++博客
首頁
發新隨筆
發新文章
聯系
聚合
管理
樹型結構
類文件:
TreeNode.java
1
package
com.bebig.hibernate.model;
2
3
import
java.util.HashSet;
4
import
java.util.Set;
5
6
import
javax.persistence.CascadeType;
7
import
javax.persistence.Entity;
8
import
javax.persistence.FetchType;
9
import
javax.persistence.GeneratedValue;
10
import
javax.persistence.Id;
11
import
javax.persistence.JoinColumn;
12
import
javax.persistence.ManyToOne;
13
import
javax.persistence.OneToMany;
14
15
@Entity
16
public
class
TreeNode
{
17
private
TreeNode parent;
18
private
int
id;
19
private
String name;
20
private
Set
<
TreeNode
>
chilrens
=
new
HashSet
<
TreeNode
>
();
21
22
@OneToMany(mappedBy
=
"
parent
"
, cascade
=
CascadeType.ALL, fetch
=
FetchType.EAGER)
23
public
Set
<
TreeNode
>
getChilrens()
{
24
return
chilrens;
25
}
26
27
public
String getName()
{
28
return
name;
29
}
30
31
@Id
32
@GeneratedValue
33
public
int
getId()
{
34
return
id;
35
}
36
37
@ManyToOne
38
@JoinColumn(name
=
"
parentId
"
)
39
public
TreeNode getParent()
{
40
return
parent;
41
}
42
43
public
void
setChilrens(Set
<
TreeNode
>
chilrens)
{
44
this
.chilrens
=
chilrens;
45
}
46
47
public
void
setName(String name)
{
48
this
.name
=
name;
49
}
50
51
public
void
setId(
int
id)
{
52
this
.id
=
id;
53
}
54
55
public
void
setParent(TreeNode parent)
{
56
this
.parent
=
parent;
57
}
58
59
}
60
測試用例:
1
package
com.bebig.hibernate.model;
2
3
import
org.hibernate.HibernateException;
4
import
org.hibernate.Session;
5
import
org.hibernate.SessionFactory;
6
7
import
org.hibernate.cfg.AnnotationConfiguration;
8
import
org.hibernate.tool.hbm2ddl.SchemaExport;
9
import
org.junit.AfterClass;
10
import
org.junit.BeforeClass;
11
import
org.junit.Test;
12
13
public
class
hibernateTest
{
14
private
static
SessionFactory sessionFactory;
15
16
@BeforeClass
17
public
static
void
beforeClass()
{
18
new
SchemaExport(
new
AnnotationConfiguration().configure()).create(
19
false
,
true
);
20
try
{
21
sessionFactory
=
new
AnnotationConfiguration().configure()
22
.buildSessionFactory();
23
}
catch
(HibernateException e)
{
24
//
TODO Auto-generated catch block
25
e.printStackTrace();
26
}
27
28
}
29
30
@Test
31
public
void
testSchemaExport()
{
32
new
SchemaExport(
new
AnnotationConfiguration().configure()).create(
33
false
,
true
);
34
35
}
36
37
@Test
38
public
void
testSave()
{
39
TreeNode t
=
new
TreeNode();
40
t.setName(
"
根結點
"
);
41
TreeNode t1
=
new
TreeNode();
42
t1.setName(
"
1結點
"
);
43
TreeNode t2
=
new
TreeNode();
44
t2.setName(
"
2結點
"
);
45
TreeNode t3
=
new
TreeNode();
46
t3.setName(
"
3結點
"
);
47
TreeNode t4
=
new
TreeNode();
48
t4.setName(
"
4結點
"
);
49
50
t1.setParent(t);
51
t2.setParent(t);
52
t3.setParent(t1);
53
t4.setParent(t2);
54
55
Session s
=
sessionFactory.getCurrentSession();
56
s.beginTransaction();
57
58
s.save(t);
59
s.save(t1);
60
s.save(t2);
61
s.save(t3);
62
s.save(t4);
63
64
s.getTransaction().commit();
65
66
}
67
68
@Test
69
public
void
testLoad()
{
70
testSave();
71
72
Session s
=
sessionFactory.getCurrentSession();
73
s.beginTransaction();
74
TreeNode t
=
(TreeNode) s.load(TreeNode.
class
,
1
);
75
print(t,
0
);
//
遞歸打印出結點信息
76
77
s.getTransaction().commit();
78
79
}
80
81
private
void
print(TreeNode t,
int
level)
{
82
String space
=
""
;
83
for
(
int
i
=
0
; i
<
level; i
++
)
{
84
space
+=
"
----
"
;
85
}
86
87
System.out.println(space
+
t.getName());
88
for
(TreeNode n : t.getChilrens())
{
89
print(n, level
+
1
);
90
}
91
92
}
93
94
@Test
95
public
void
testDelete()
{
96
testSave();
97
98
Session s
=
sessionFactory.getCurrentSession();
99
s.beginTransaction();
100
TreeNode t
=
(TreeNode) s.load(TreeNode.
class
,
2
);
101
s.delete(t);
102
s.getTransaction().commit();
103
104
}
105
106
@AfterClass
107
public
static
void
afterClass()
{
108
sessionFactory.close();
109
}
110
111
public
static
void
main(String[] args)
{
112
beforeClass();
113
}
114
}
115
發表于 2010-09-29 08:45
風林
閱讀(171)
評論(0)
編輯
收藏
引用
所屬分類:
JAVA
、
Hibernate
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
[轉]java編程中'為了性能'一些盡量做到的地方
JAVAC命令行錯誤
Java基礎_Collection接口下的子類存儲特性
Android_常用控件使用
Android_Activity&Intent&Layout
Integer自動裝箱、拆箱問題
自定義比較器、排序
JDBC操作實例
常用數據庫的JDBC連接代碼
[轉]J2SE_內部類
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
CALENDER
<
2025年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
給我留言
查看公開留言
查看私人留言
隨筆分類
jBPM
(rss)
隨筆檔案
2011年7月 (1)
2011年4月 (1)
2011年2月 (1)
2010年11月 (1)
2010年10月 (2)
2010年9月 (3)
2010年8月 (4)
2010年7月 (2)
文章分類
AJAX(2)
(rss)
Android(2)
(rss)
C#(20)
(rss)
C++(6)
(rss)
ckeditor&ckfinder(1)
(rss)
CSS
(rss)
Delphi(2)
(rss)
Hibernate(39)
(rss)
JAVA(95)
(rss)
jQuery(1)
(rss)
JSP(9)
(rss)
Maven(1)
(rss)
MySQL(4)
(rss)
OOP(1)
(rss)
Python(42)
(rss)
Spring(31)
(rss)
SQL Server(4)
(rss)
Struts2(35)
(rss)
SVN(1)
(rss)
Tomcat(1)
(rss)
Ubuntu(1)
(rss)
軟件加解密技術
(rss)
雜文(1)
(rss)
文章檔案
2011年8月 (1)
2011年7月 (3)
2011年6月 (19)
2011年5月 (2)
2011年4月 (1)
2011年2月 (1)
2010年12月 (2)
2010年11月 (21)
2010年10月 (67)
2010年9月 (48)
2010年8月 (37)
2010年7月 (4)
新聞檔案
2010年7月 (1)
相冊
CSS
Hibernate
搜索
最新評論
1.?re: Struts2_三種傳參數方法
方式的發生
--阿飛史蒂夫
2.?re: 在Win7上搭建JSP開發環境
評論內容較長,點擊標題查看
--鄒
3.?re: ckeditor&ckfinder&s2sh集成
評論內容較長,點擊標題查看
--庸幾何
4.?re: 在Win7上搭建JSP開發環境
下個 myeclipse@lou
--孫毅
5.?re: 在Win7上搭建JSP開發環境
@lou
運行 -cmd 找到startup.bat 在java 環境中運行
--孫毅
閱讀排行榜
1.?開始找Java開發類工作了(504)
2.?給一家公司的軟件做加密方案(442)
3.?周一到周五都得出差,周末才能回(416)
4.?一流、二流、三流(408)
5.?最近工作有點忙(377)
評論排行榜
1.?周一到周五都得出差,周末才能回(2)
2.?給力2011(2)
3.?最近工作有點忙(0)
4.?生活(0)
5.?在博客園開博了(0)
Powered By:
博客園
模板提供
:
滬江博客
国产69精品久久久久9999
|
久久久久久狠狠丁香
|
99久久99这里只有免费的精品
|
久久精品国产亚洲av日韩
|
久久午夜无码鲁丝片午夜精品
|
久久精品国产久精国产一老狼
|
日韩欧美亚洲综合久久影院d3
|
91精品国产高清久久久久久国产嫩草
|
丁香色欲久久久久久综合网
|
久久综合欧美成人
|
波多野结衣久久一区二区
|
久久人人爽人人精品视频
|
欧美日韩精品久久免费
|
久久狠狠色狠狠色综合
|
国产精品天天影视久久综合网
|
国产精品美女久久久网AV
|
久久综合鬼色88久久精品综合自在自线噜噜
|
国产69精品久久久久9999
|
久久久久国产精品嫩草影院
|
久久精品国产亚洲欧美
|
影音先锋女人AV鲁色资源网久久
|
青青草国产精品久久久久
|
伊人久久久AV老熟妇色
|
久久久久久噜噜精品免费直播
|
99久久精品无码一区二区毛片
|
欧美久久久久久
|
亚洲国产小视频精品久久久三级
|
亚洲国产婷婷香蕉久久久久久
|
久久综合狠狠色综合伊人
|
久久精品国产亚洲av水果派
|
亚洲级αV无码毛片久久精品
|
伊人久久精品无码二区麻豆
|
久久99国产精品成人欧美
|
久久综合九色综合网站
|
久久精品无码一区二区三区日韩
|
亚洲第一永久AV网站久久精品男人的天堂AV
|
久久久久久久女国产乱让韩
|
久久久国产一区二区三区
|
久久99国产一区二区三区
|
久久强奷乱码老熟女
|
思思久久99热只有频精品66
|