歡迎來到小米的博客
希望能和您交流Java編程的知識(shí)和見解
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
隨筆-57 評(píng)論-202 文章-17 trackbacks-0
Hibernate的一對(duì)一關(guān)聯(lián)實(shí)例
Hibernate中的表的關(guān)聯(lián)有一對(duì)一,一對(duì)多和多對(duì)多三種關(guān)聯(lián)方式,在這篇筆記和接下來的筆記中,我將用我自己的實(shí)際例子來說明如何具體實(shí)施。
我使用的Hibernate版本是2.1.8,在Hibernate的網(wǎng)站2.1.6版本的中文文檔中有關(guān)一對(duì)一的關(guān)聯(lián)有下面一段表述:
5.1.11. 一對(duì)一
持久化對(duì)象之間一對(duì)一的關(guān)聯(lián)關(guān)系是通過one-to-one元素定義的。
<
one-to-one
name
="propertyName"
(1)
class
="ClassName"
(2)
cascade
="all|none|save-update|delete"
(3)
constrained
="true|false"
(4)
outer-join
="true|false|auto"
(5)
property-ref
="propertyNameFromAssociatedClass"
(6)
access
="field|property|ClassName"
(7)
/>
(1) name: 屬性的名字。
(2) class (可選 - 默認(rèn)是通過反射得到的屬性類型):被關(guān)聯(lián)的類的名字。
(3) cascade(級(jí)聯(lián)) (可選) 表明操作是否從父對(duì)象級(jí)聯(lián)到被關(guān)聯(lián)的對(duì)象。
(4) constrained(約束) (可選) 表明該類對(duì)應(yīng)的表對(duì)應(yīng)的數(shù)據(jù)庫表,和被關(guān)聯(lián)的對(duì)象所對(duì)應(yīng)的數(shù)據(jù)庫表之間,通過一個(gè)外鍵引用對(duì)主鍵進(jìn)行約束。這個(gè)選項(xiàng)影響save()和delete()在級(jí)聯(lián)執(zhí)行時(shí)的先后順序(也在schema export tool中被使用)。
(5) outer-join(外連接) (可選 - 默認(rèn)為 自動(dòng)): 當(dāng)設(shè)置hibernate.use_outer_join的時(shí)候,對(duì)這個(gè)關(guān)聯(lián)允許外連接抓取。
(6) property-ref: (可選) 指定關(guān)聯(lián)類的一個(gè)屬性,這個(gè)屬性將會(huì)和本外鍵相對(duì)應(yīng)。如果沒有指定,會(huì)使用對(duì)方關(guān)聯(lián)類的主鍵。
(7) access (可選 - 默認(rèn)是 property): Hibernate用來訪問屬性的策略。
有兩種不同的一對(duì)一關(guān)聯(lián):
主鍵關(guān)聯(lián)
惟一外鍵關(guān)聯(lián)
主鍵關(guān)聯(lián)不需要額外的表字段;兩行是通過這種一對(duì)一關(guān)系相關(guān)聯(lián)的,那么這兩行就共享同樣的主關(guān)鍵字值。所以如果你希望兩個(gè)對(duì)象通過主鍵一對(duì)一關(guān)聯(lián),你必須確認(rèn)它們被賦予同樣的標(biāo)識(shí)值!
比如說,對(duì)下面的Employee和Person進(jìn)行主鍵一對(duì)一關(guān)聯(lián):
<
one-to-one
name
="person"
class
="Person"
/>
<
one-to-one
name
="employee"
class
="Employee"
constrained
="true"
/>
現(xiàn)在我們必須確保PERSON和EMPLOYEE中相關(guān)的字段是相等的。我們使用一個(gè)特別的稱為foreign的Hibernate標(biāo)識(shí)符生成器策略:
<
class
name
="person"
table
="PERSON"
>
<
id
name
="id"
column
="PERSON_ID"
>
<
generator
class
="foreign"
>
<
param
name
="property"
>
employee
</
param
>
</
generator
>
</
id
>
<
one-to-one
name
="employee"
class
="Employee"
constrained
="true"
/>
</
class
>
一個(gè)剛剛保存的Person實(shí)例被賦予和該P(yáng)erson的employee屬性所指向的Employee實(shí)例同樣的關(guān)鍵字值。
另一種方式是一個(gè)外鍵和一個(gè)惟一關(guān)鍵字對(duì)應(yīng),上面的Employee和Person的例子,如果使這種關(guān)聯(lián)方式,應(yīng)該表達(dá)成:
<
many-to-one
name
="person"
class
="Person"
column
="PERSON_ID"
unique
="true"
/>
如果在Person的映射加入下面幾句,這種關(guān)聯(lián)就是雙向的:
<
one-to-one
name"employee" class
="Employee"
property-ref
="person"
/>
下面是我的一個(gè)一對(duì)一主鍵關(guān)聯(lián)的例子,使用的數(shù)據(jù)庫是MySQL 4.1.11:
我有兩個(gè)表:UserBasic和UserInfo,UserBasic記錄的是用戶的基本注冊(cè)信息,UserInfo表記錄的是用戶的詳細(xì)信息。表的結(jié)構(gòu)如下:
1
CREATE
TABLE
IF
NOT
EXISTS
UserBasic
2
(
3
Guid
INT
NOT
NULL
AUTO_INCREMENT,
4
Account
VARCHAR
(
64
)
NOT
NULL
,
5
Password
VARCHAR
(
16
)
NOT
NULL
,
6
Email
VARCHAR
(
128
)
NOT
NULL
,
7
PRIMARY
KEY
(Guid)
8
) TYPE
=
InnoDB;
9
10
CREATE
TABLE
IF
NOT
EXISTS
UserInfo
11
(
12
Guid
INT
NOT
NULL
,
13
Username
VARCHAR
(
128
),
14
Gender
CHAR
(
1
),
15
Birthday
DATETIME
,
16
PRIMARY
KEY
(Guid)
17
) TYPE
=
InnoDB;
18
19
ALTER
TABLE
UserInfo
ADD
CONSTRAINT
UserInfoRFUserBasic
FOREIGN
KEY
(Guid)
20
REFERENCES
UserBasic (Guid)
ON
DELETE
CASCADE
ON
UPDATE
RESTRICT
;
UserInfo的主鍵值和UserBasic的主鍵值是一樣的,兩個(gè)表是單向的一對(duì)一關(guān)系。UserBasic為主控方,UserInfo是被動(dòng)方。
用Middlegen生成的UserBasic.hbm.xml文件,修改后的內(nèi)容如下:
1
<?
xml version="1.0"
?>
2
<!
DOCTYPE hibernate-mapping PUBLIC
3
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
5
6
<
hibernate-mapping
>
7
<!--
8
Created by the Middlegen Hibernate plugin 2.1
9
10
http://boss.bekk.no/boss/middlegen/
11
http://www.hibernate.org/
12
-->
13
14
<
class
15
name
="com.xxx.hibernate.UserBasic"
16
table
="UserBasic"
17
dynamic-update
="true"
18
dynamic-insert
="true"
19
>
20
<
meta
attribute
="class-description"
inherit
="false"
>
21
@hibernate.class
22
table="UserBasic"
23
dynamic-update="true"
24
dynamic-insert="true"
25
</
meta
>
26
27
<
id
28
name
="guid"
29
type
="int"
30
column
="Guid"
31
>
32
<
meta
attribute
="field-description"
>
33
@hibernate.id
34
generator-class="native"
35
type="int"
36
column="Guid"
37
38
39
</
meta
>
40
<
generator
class
="native"
/>
41
</
id
>
42
43
<
property
44
name
="account"
45
type
="java.lang.String"
46
column
="Account"
47
not-null
="true"
48
length
="64"
49
>
50
<
meta
attribute
="field-description"
>
51
@hibernate.property
52
column="Account"
53
length="64"
54
not-null="true"
55
</
meta
>
56
</
property
>
57
<
property
58
name
="password"
59
type
="java.lang.String"
60
column
="Password"
61
not-null
="true"
62
length
="16"
63
>
64
<
meta
attribute
="field-description"
>
65
@hibernate.property
66
column="Password"
67
length="16"
68
not-null="true"
69
</
meta
>
70
</
property
>
71
<
property
72
name
="email"
73
type
="java.lang.String"
74
column
="Email"
75
not-null
="true"
76
length
="128"
77
>
78
<
meta
attribute
="field-description"
>
79
@hibernate.property
80
column="Email"
81
length="128"
82
not-null="true"
83
</
meta
>
84
</
property
>
85
86
<!--
Associations
-->
87
88
<!--
bi-directional one-to-one association to UserInfo
-->
89
<
one-to-one
90
name
="userInfo"
91
class
="com.xxx.hibernate.UserInfo"
92
cascade
="save-update"
93
>
94
<
meta
attribute
="field-description"
>
95
@hibernate.one-to-one
96 class="com.xxx.hibernate.UserInfo"
97
cascade="save-update"
98
</
meta
>
99
</
one-to-one
>
100
101
</
class
>
102
</
hibernate-mapping
>
由于在建立外鍵的時(shí)候就聲明了ON DELETE CASCADE,所以在xml的配置文件中第97行聲明為save-update。如果聲明為all,那么在刪除UserBasic表的數(shù)據(jù)時(shí),會(huì)無謂的多出一條刪除UserInfo的delete語句出來。
UserInfo.hbm.xml文件的內(nèi)容如下:
1
<?
xml version
=
"
1.0
"
?>
2
<!
DOCTYPE hibernate
-
mapping PUBLIC
3
"
-//Hibernate/Hibernate Mapping DTD 2.0//EN
"
4
"
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd
"
>
5
6
<
hibernate
-
mapping
>
7
<!--
8
Created by the Middlegen Hibernate plugin
2.1
9
10
http:
//
boss.bekk.no/boss/middlegen/
11
http:
//
www.hibernate.org/
12
-->
13
14
<
class
15
name
=
"
com.xxx.hibernate.UserInfo
"
16
table
=
"
UserInfo
"
17
dynamic
-
update
=
"
true
"
18
dynamic
-
insert
=
"
true
"
19
>
20
<
meta attribute
=
"
class-description
"
inherit
=
"
false
"
>
21
@hibernate.
class
22
table
=
"
UserInfo
"
23
dynamic
-
update
=
"
true
"
24
dynamic
-
insert
=
"
true
"
25
</
meta
>
26
27
<
id
28
name
=
"
guid
"
29
type
=
"
int
"
30
column
=
"
Guid
"
31
>
32
<
meta attribute
=
"
field-description
"
>
33
@hibernate.id
34
generator
-
class
=
"
foreign
"
35
type
=
"
int
"
36
column
=
"
Guid
"
37
38
39
</
meta
>
40
<
generator
class
=
"
foreign
"
>
41
<
param name
=
"
property
"
>
userBasic
</
param
>
42
</
generator
>
43
</
id
>
44
45
<
property
46
name
=
"
username
"
47
type
=
"
java.lang.String
"
48
column
=
"
Username
"
49
length
=
"
128
"
50
>
51
<
meta attribute
=
"
field-description
"
>
52
@hibernate.property
53
column
=
"
Username
"
54
length
=
"
128
"
55
</
meta
>
56
</
property
>
57
<
property
58
name
=
"
gender
"
59
type
=
"
java.lang.String
"
60
column
=
"
Gender
"
61
length
=
"
1
"
62
>
63
<
meta attribute
=
"
field-description
"
>
64
@hibernate.property
65
column
=
"
Gender
"
66
length
=
"
1
"
67
</
meta
>
68
</
property
>
69
<
property
70
name
=
"
birthday
"
71
type
=
"
java.sql.Date
"
72
column
=
"
Birthday
"
73
length
=
"
19
"
74
>
75
<
meta attribute
=
"
field-description
"
>
76
@hibernate.property
77
column
=
"
Birthday
"
78
length
=
"
19
"
79
</
meta
>
80
</
property
>
81
82
<!--
Associations
-->
83
84
<!--
bi
-
directional one
-
to
-
one association to UserBasic
-->
85
<
one
-
to
-
one
86
name
=
"
userBasic
"
87
class
=
"
com.xxx.hibernate.UserBasic
"
88
constrained
=
"
true
"
89
>
90
<
meta attribute
=
"
field-description
"
>
91
@hibernate.one
-
to
-
one
92
class
=
"
com.xxx.hibernate.UserBasic
"
93
constrained
=
"
true
"
94
</
meta
>
95
</
one
-
to
-
one
>
96
97
</
class
>
98
</
hibernate
-
mapping
>
用hbm2java生成對(duì)應(yīng)的對(duì)應(yīng)的Java類:hbm2java *.xml --output=xxx。
Hibernate的配置文件hibernate.cfg.xml內(nèi)容如下:
1
<?
xml version="1.0" encoding="utf-8"
?>
2
<!
DOCTYPE hibernate-configuration
3
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"
>
5
6
<
hibernate-configuration
>
7
<
session-factory
>
8
9
<!--
local connection properties
-->
10
<
property
name
="hibernate.connection.url"
>
jdbc:mysql://127.0.0.1/xxx?useUnicode=true
&
characterEncoding=UTF-8
&
autoReconnect=true
</
property
>
11
<
property
name
="hibernate.connection.driver_class"
>
com.mysql.jdbc.Driver
</
property
>
12
<
property
name
="hibernate.connection.username"
>
root
</
property
>
13
<
property
name
="hibernate.connection.password"
>
123456
</
property
>
14
<!--
property name="hibernate.connection.pool_size"></property
-->
15
16
<!--
dialect for MySQL
-->
17
<
property
name
="dialect"
>
net.sf.hibernate.dialect.MySQLDialect
</
property
>
18
19
<
property
name
="hibernate.show_sql"
>
true
</
property
>
20
<
property
name
="hibernate.use_outer_join"
>
true
</
property
>
21
<
property
name
="hibernate.transaction.factory_class"
>
net.sf.hibernate.transaction.JDBCTransactionFactory
</
property
>
22
23
<
mapping
resource
="com/xxx/hibernate/UserBasic.hbm.xml"
/>
24
<
mapping
resource
="com/xxx/hibernate/UserInfo.hbm.xml"
/>
25
26
</
session-factory
>
27
</
hibernate-configuration
>
JUnit的測(cè)試用例程序片斷如下:
1
public
void
testInsertUser() throws Exception
{
2
UserBasic user
=
new
UserBasic();
3
user.setAccount(
"
test
"
);
4
user.setPassword(
"
123456
"
);
5
user.setEmail(
"
georgehill@21cn.com
"
);
6
7
UserInfo info
=
new
UserInfo();
8
info.setUsername(
"
George Hill
"
);
9
info.setGender(
"
M
"
);
10
info.setBirthday(
new
Date());
11
12
user.setUserInfo(info);
13
info.setUserBasic(user);
14
15
Transaction tx
=
session.beginTransaction();
16
session.save(user);
17
tx.commit();
18
}
運(yùn)行測(cè)試程序,可以看到輸出了兩條insert語句。
posted on 2005-05-14 15:02
小米
閱讀(4062)
評(píng)論(2)
編輯
收藏
所屬分類:
Hibernate
評(píng)論:
#
re: Hibernate的一對(duì)一關(guān)聯(lián)實(shí)例 2014-10-16 16:03 |
vds
好樣的
回復(fù)
更多評(píng)論
#
re: Hibernate的一對(duì)一關(guān)聯(lián)實(shí)例
2016-06-17 15:32 |
33
根據(jù)寫了報(bào)錯(cuò)了是怎么回事
回復(fù)
更多評(píng)論
新用戶注冊(cè)
刷新評(píng)論列表
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
《深入淺出Hibernate》讀書筆記(9)——Session管理
《深入淺出Hibernate》讀書筆記(8)——Hibernate分頁
《深入淺出Hibernate》讀書筆記(7)——回調(diào)與攔截機(jī)制
《深入淺出Hibernate》讀書筆記(6)——集合類型和結(jié)果集排序
《深入淺出Hibernate》讀書筆記(5)——持久層操作
《深入淺出Hibernate》讀書筆記(4)——事務(wù)管理
《深入淺出Hibernate》讀書筆記(3)——數(shù)據(jù)緩存
《深入淺出Hibernate》讀書筆記(2)——實(shí)體對(duì)象識(shí)別
《深入淺出Hibernate》讀書筆記(1)——實(shí)體對(duì)象生命周期
用HQL獲取部分的實(shí)體對(duì)象屬性
小米,生活在深圳,專注于Java,主要從事數(shù)據(jù)庫和網(wǎng)頁編程。現(xiàn)在在學(xué)習(xí)著Hibernate和Spring。喜歡游戲、音樂和臺(tái)球。聯(lián)系方式:georgehill@21cn.com
<
2014年10月
>
日
一
二
三
四
五
六
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
8
常用鏈接
我的隨筆
我的評(píng)論
我的參與
最新評(píng)論
留言簿
(27)
給我留言
查看公開留言
查看私人留言
隨筆分類
Hibernate(15)
Java(17)
Spring(1)
Struts(5)
其它(5)
數(shù)據(jù)庫(2)
生活隨筆(12)
隨筆檔案
2006年4月 (1)
2006年3月 (1)
2005年8月 (1)
2005年7月 (11)
2005年6月 (13)
2005年5月 (30)
文章分類
Eclipse(1)
Java(8)
其它(8)
文章檔案
2005年7月 (1)
2005年6月 (13)
2005年5月 (3)
我的朋友們
emu的博客
Java BY
我的鏈接
Java Research
SUN Java技術(shù)中文社區(qū)
拯救程序員王俊
搜索
積分與排名
積分 - 234205
排名 - 247
最新評(píng)論
1.?re: Hibernate的一對(duì)一關(guān)聯(lián)實(shí)例
根據(jù)寫了報(bào)錯(cuò)了是怎么回事
--33
2.?re: 用java.util.Timer定時(shí)執(zhí)行任務(wù)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--yunp
3.?re: Hibernate的一對(duì)一關(guān)聯(lián)實(shí)例
好樣的
--vds
4.?re: 如何在Struts中實(shí)現(xiàn)分頁顯示數(shù)據(jù)(1)
PageData中的集合是所有都取出,還是用多少取多少,若是前者,會(huì)拖慢系統(tǒng)的。
--李亞男
5.?re: BMP文件格式
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--見面
閱讀排行榜
1.?用java.util.Timer定時(shí)執(zhí)行任務(wù)(33758)
2.?用JFreeChart畫柱狀圖的范例(10708)
3.?《深入淺出Hibernate》讀書筆記(3)——數(shù)據(jù)緩存(6122)
4.?《深入淺出Hibernate》讀書筆記(8)——Hibernate分頁(5566)
5.?用ChartDirector在JSP中畫統(tǒng)計(jì)圖(5256)
評(píng)論排行榜
1.?如何在Struts中實(shí)現(xiàn)分頁顯示數(shù)據(jù)(2)(25)
2.?獻(xiàn)出一份愛心 共同援助重病程序員王俊(22)
3.?Struts的國(guó)際化完整解決方案(11)
4.?2005年6月27日,一個(gè)值得紀(jì)念的日子(9)
5.?《深入淺出Hibernate》讀書筆記(1)——實(shí)體對(duì)象生命周期(9)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 小米
主站蜘蛛池模板:
亚洲av永久无码精品秋霞电影影院
|
亚洲成在人线aⅴ免费毛片
|
成人免费网站视频www
|
亚洲已满18点击进入在线观看
|
综合久久久久久中文字幕亚洲国产国产综合一区首
|
成人精品视频99在线观看免费
|
亚洲最大的成人网站
|
亚洲图片一区二区
|
中文字幕亚洲综合久久男男
|
日韩在线a视频免费播放
|
蜜桃AV无码免费看永久
|
日本视频免费高清一本18
|
一级成人毛片免费观看
|
久久亚洲色WWW成人欧美
|
亚洲国产91在线
|
亚洲成在人线电影天堂色
|
亚洲国产精品无码中文字
|
中文字幕亚洲激情
|
亚洲人成影院在线观看
|
国产gav成人免费播放视频
|
全免费a级毛片免费看不卡
|
亚洲天堂免费在线
|
日韩精品免费一级视频
|
99re6热视频精品免费观看
|
久久久久久久岛国免费播放
|
免费看成人AA片无码视频吃奶
|
精品国产免费人成网站
|
日韩在线一区二区三区免费视频
|
99亚洲男女激情在线观看
|
亚洲欧洲日产国码久在线
|
在线亚洲午夜片AV大片
|
亚洲人成www在线播放
|
中文字幕亚洲情99在线
|
中国亚洲呦女专区
|
精品亚洲视频在线
|
在线亚洲精品视频
|
手机永久免费的AV在线电影网
|
午夜肉伦伦影院久久精品免费看国产一区二区三区
|
亚洲制服丝袜精品久久
|
久久亚洲最大成人网4438
|
亚洲xxxx视频
|