?? 我寫的這些只是Hibernate的一些基本的使用方法,如果你不是剛剛學Hibernate的話,這篇東東不再適合你看了,呵呵。我使用的是eclipse3.2.1+myeclipse5.1.1+Firebird 2.0,Firebrid 2.0 你可以從www.Firebirdsql.org上下載到最新版本的,如果你不想用這個數據庫,你也可換成別的數據庫。
?建表語句:
create database 'd:\sovo.fdb' user 'SYSDBA' password 'masterkey';
create? domain d_text as blob sub_type 1;
create table Testdomain(
id Integer primary key,
name varchar(30),
context d_text
);
打開Firebird數據庫的aliases.conf,
在里面配置: sovo = d:\sovo.fdb
在這里配置,主要是為了簡化在hibernate.cfg.xml里設connection.url
數據庫配置文件 hibernate.cfg.xml
?1
<?
xml?version
=
'
1.0
'
?encoding
=
'
UTF-8
'
?>
?2
<!
DOCTYPE?hibernate
-
configuration?PUBLIC
?3
??????????
"
-//Hibernate/Hibernate?Configuration?DTD?3.0//EN
"
?4
??????????
"
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd
"
>
?5
?6
<!--
?Generated?by?MyEclipse?Hibernate?Tools.???????????????????
-->
?7
<
hibernate
-
configuration
>
?8
?9
????
<
session
-
factory
>
10
????????
<
property?name
=
"
dialect
"
>
11
????????????org.hibernate.dialect.FirebirdDialect
12
????????
</
property
>
13
????????
<
property?name
=
"
connection.url
"
>
14
????????????jdbc:firebirdsql:
//
localhost:3050/sovo
15
????????
</
property
>
16
????????
<
property?name
=
"
connection.username
"
>
SYSDBA
</
property
>
17
????????
<
property?name
=
"
connection.password
"
>
masterkey
</
property
>
18
????????
<
property?name
=
"
connection.driver_class
"
>
19
????????????org.firebirdsql.jdbc.FBDriver
20
????????
</
property
>
21
????????
<
property?name
=
"
myeclipse.connection.profile
"
>
22
????????????Firebird
23
????????
</
property
>
24
????????
<
mapping?resource
=
"
com/datamodel/Testdomain.hbm.xml
"
?
/>
25
26
????
</
session
-
factory
>
27
28
</
hibernate
-
configuration
>

?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

數據映射文件 Testdomain.hbm.xml
?1
<?
xml?version
=
"
1.0
"
?encoding
=
"
utf-8
"
?>
?2
<!
DOCTYPE?hibernate
-
mapping?PUBLIC?
"
-//Hibernate/Hibernate?Mapping?DTD?3.0//EN
"
?3
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
"
>
?4
<!--
?
?5
????Mapping?file?autogenerated?by?MyEclipse?
-
?Hibernate?Tools
?6
-->
?7
<
hibernate
-
mapping
>
?8
????
<
class
?name
=
"
com.datamodel.Testdomain
"
?table
=
"
TESTDOMAIN
"
>
?9
????????
<
id?name
=
"
id
"
?type
=
"
java.lang.Integer
"
>
10
????????????
<
column?name
=
"
ID
"
?
/>
11
????????????
<
generator?
class
=
"
increment
"
?
/>
12
????????
</
id
>
13
????????
<
property?name
=
"
name
"
?type
=
"
java.lang.String
"
>
14
????????????
<
column?name
=
"
NAME
"
?length
=
"
30
"
?
/>
15
????????
</
property
>
16
????????
<
property?name
=
"
context
"
?type
=
"
java.lang.String
"
>
17
????????????
<
column?name
=
"
CONTEXT
"
?length
=
"
0
"
?
/>
18
????????
</
property
>
19
????
</
class
>
20
</
hibernate
-
mapping
>
21

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

持久化類 Testdomain.java
?1
package
?com.datamodel;
?2
?3
?4
/**?*/
/**
?5
?*?Testdomain?generated?by?MyEclipse?-?Hibernate?Tools
?6
?
*/
?7
?8
public
?
class
?Testdomain??
implements
?java.io.Serializable?
{
?9
10
11
????
//
?Fields????
12
13
?????
/**?*/
/**
14
?????*?
15
?????
*/
16
????
private
?
static
?
final
?
long
?serialVersionUID?
=
?
1L
;
17
????
private
?Integer?id;
18
?????
private
?String?name;
19
?????
private
?String?context;
20
21
22
????
//
?Constructors
23
24
????
/**?*/
/**
?default?constructor?
*/
25
????
public
?Testdomain()?
{
26
????}
27
28
????
29
????
/**?*/
/**
?full?constructor?
*/
30
????
public
?Testdomain(String?name,?String?context)?
{
31
????????
this
.name?
=
?name;
32
????????
this
.context?
=
?context;
33
????}
34
35
???
36
????
//
?Property?accessors
37
38
????
public
?Integer?getId()?
{
39
????????
return
?
this
.id;
40
????}
41
????
42
????
public
?
void
?setId(Integer?id)?
{
43
????????
this
.id?
=
?id;
44
????}
45
46
????
public
?String?getName()?
{
47
????????
return
?
this
.name;
48
????}
49
????
50
????
public
?
void
?setName(String?name)?
{
51
????????
this
.name?
=
?name;
52
????}
53
54
????
public
?String?getContext()?
{
55
????????
return
?
this
.context;
56
????}
57
????
58
????
public
?
void
?setContext(String?context)?
{
59
????????
this
.context?
=
?context;
60
????}
61
???
62
63
}

?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

32

33

34

35

36

37

38



39

40

41

42



43

44

45

46



47

48

49

50



51

52

53

54



55

56

57

58



59

60

61

62

63

測試類TestHibernate.java,對數據進行讀寫操作
?1
/**?*/
/**
?2
?*?
?3
?
*/
?4
package
?com.test;
?5
?6
import
?java.util.List;
?7
?8
import
?org.hibernate.Session;
?9
import
?org.hibernate.SessionFactory;
10
import
?org.hibernate.Transaction;
11
import
?org.hibernate.cfg.Configuration;
12
13
import
?com.datamodel.HibernateSessionFactory;
14
import
?com.datamodel.Testdomain;
15
16
import
?junit.framework.TestCase;
17
18
/**?*/
/**
19
?*?
@author
?dragon
20
?*
21
?
*/
22
public
?
class
?TestHiberante?
extends
?TestCase
{
23
????
24
????
private
?Session?session;
25
????
private
?Transaction?tx;
26
????
27
????
protected
?
void
?setUp()
{
28
????????
29
????????
try
{
30
????????Configuration?cfg?
=
?
new
?Configuration().configure(
"
/com/datamodel/hibernate.cfg.xml
"
);
31
????????SessionFactory?sf?
=
?cfg.buildSessionFactory();?
32
??????????session?
=
?sf.openSession();
33
??????????tx?
=
?session.beginTransaction();
34
????????}
catch
?(Exception?e)
{
35
????????????e.printStackTrace();
36
????????}
????
37
????
38
????}
39
//
???保存數據
40
????
public
?
void
?testSave()
{
41
????????Testdomain?td?
=
?
new
?Testdomain();
42
????????td.setName(
"
dragon
"
);
43
????????td.setContext(
"
我愛北京天安門!
"
);
44
????????
45
????????session.save(td);
46
????????????
47
????}
48
//
????更新數據
49
????
public
?
void
?_testUpdate()
{
50
???????Integer?id?
=
new
?Integer(
1
);
51
???????Testdomain?td?
=
(Testdomain)?session.get(Testdomain.
class
,?id);
52
???????td.setName(
"
javadragon
"
);
53
???????session.update(td);
54
????}
55
????
56
//
????查詢全部數據
57
????
public
?
void
?testLoad()
{
58
??????List?list
=
?session.createQuery(
"
from?Testdomain
"
).list();
59
??????
for
?(
int
?i?
=
0
;?i?
<
?list.size();?i
++
)
{
60
??????????Testdomain?td?
=
(Testdomain)?list.get(i);
61
??????????
62
???????????System.out.println(td.getName()
+
"
??
"
+
td.getContext());?
63
??????}
64
????}
65
//
????刪除指定的某條數據
66
????
public
?
void
?testDelete()
{
67
???????Integer?id?
=
?
new
?Integer(
1
);
68
???????Testdomain?td?
=
?(Testdomain)?session.get(Testdomain.
class
,?id);
69
???????session.delete(td);
70
????}
71
????
72
????
73
????
protected
?
void
?tearDwon()
{
74
????????tx.commit();
75
????????session.close();
76
????}
77
}
78


?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

32

33

34



35

36

37

38

39

40



41

42

43

44

45

46

47

48

49



50

51

52

53

54

55

56

57



58

59



60

61

62

63

64

65

66



67

68

69

70

71

72

73



74

75

76

77

78
