Hibernate創(chuàng)建數據庫表(一對一and一對多)
使用的數據庫是MSSQL,庫名hibernate,預建立的表有3張。分別是Student(學生)表,字段名:id、team_di、name、cardId、age。
team(班級)表,字段名:id、team_id。
Certificate(身份證)表,字段名:id、describe。
Student與Certificate是一對一的關系,team與Student是一對多的關系。
1.建立工程->加入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.SQLServerDialect
12
</property>
13
<property name="connection.url">
14
jdbc:microsoft:sqlserver://localhost:1433;databasename=hibernate
15
</property>
16
<property name="connection.username">sa</property>
17
<property name="connection.password">sa</property>
18
<property name="connection.driver_class">
19
com.microsoft.jdbc.sqlserver.SQLServerDriver
20
</property>
21
<property name="myeclipse.connection.profile">MsSQL</property>
22
23
<property name="hibernate.hbm2ddl.auto">create-drop</property>
24
<property name="show_sql">true</property>
25
26
27
<mapping resource="com/zzn/hibernate1/Certificate.hbm.xml" />
28
<mapping resource="com/zzn/hibernate1/Student.hbm.xml" />
29
<mapping resource="com/zzn/hibernate1/Team.hbm.xml" />
30
31
32
</session-factory>
33
34
</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

29

30

31

32

33

34

14行的databasename=hibernate是你要讓hibernate把數據庫表建立到那個數據庫里,需要你來指定。如果不指定就加到默認的庫中。
23行的<property name="hibernate.hbm2dll.auro"></property>千萬不要忘寫。中間的參數可以是create-drop或update,如果你建立的表名在數據庫中已有,那么create-drop是將它刪然后建立你用hibernate要建立的表,而update則是更新你已有的數據庫表,原有的列都不會被刪除。
24行是在console中顯示執(zhí)行的SQL語句。
27-29行是即將要建立的映射文件。
2.下面我們要編寫映射文件。
Student.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
<hibernate-mapping>
5
<class name="com.zzn.hibernate1.Student" table="student" lazy="true">
6
<id name="id" unsaved-value="null">
7
<generator class="uuid.hex"></generator>
8
</id>
9
<property name="cardid" type="string"></property>
10
<property name="name" type="string"></property>
11
<property name="age" type="int"></property>
12
<one-to-one name="cer" class="com.zzn.hibernate1.Certificate"
13
fetch="join" cascade="all">
14
</one-to-one>
15
<many-to-one name="team" class="com.zzn.hibernate1.Team" column="team_id" fetch="join" cascade="all>
16
</many-to-one>
17
</class>
18
</hibernate-mapping>
Certificate.hbm.xml
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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
<hibernate-mapping>
5
<class name="com.zzn.hibernate1.Certificate" table="certificate" lazy="true">
6
<id name="id">
7
<generator class="foreign">
8
<param name="property">stu</param>
9
</generator>
10
</id>
11
<one-to-one name="stu" class="com.zzn.hibernate1.Student" fetch="join" constrained="true">
12
</one-to-one>
13
<property name="describe" column="describes" type="string"></property>
14
</class>
15
</hibernate-mapping>
Team.hbm.xml
2

3

4

5

6

7

8

9

10

11

12

13

14

15

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
<hibernate-mapping>
5
<class name="com.zzn.hibernate1.Team" table="team" lazy="true">
6
<id name="id" unsaved-value="null">
7
<generator class="uuid.hex">
8
</generator>
9
</id>
10
<property name="teamName" type="string"></property>
11
<set name="students" inverse="true" fetch="select" lazy="true">
12
<key column="team_id"></key>
13
<one-to-many class="com.zzn.hibernate1.Student"/>
14
</set>
15
</class>
16
</hibernate-mapping>
編寫持久化類
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Student.java



























Test.java













































posted on 2008-07-25 11:02 生命的綻放 閱讀(3266) 評論(2) 編輯 收藏 所屬分類: Hibernate