Sunarrow
记录Sunarrow的Java之路
导航
BlogJava
首页
新随笔
联系
聚合
管理
统计信息
Posts - 1
Stories - 0
Comments - 11
Trackbacks - 0
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔分类
Hibernate(1)
(rss)
JSF
(rss)
Servlet/JSP
(rss)
Struts
(rss)
随笔档案
2005年6月 (1)
搜索
积分与排名
积分 - 981
排名 - 4685
最新评论
1. re: Hibernate的映射
非常好的总结,通过它我今天终于把一对一,一对多,多对一,多对多搞清楚了。以后就不用看着书上的例子配关系了。好开心呀。
--spiderman
Hibernate的映射
本人觉得开始学Hibernate,它的映射关系很是麻烦,写映射文件极为容易出错。所以就把映射表各元素详细解释。以记之。
这个示例是一个很简单的数据表结构,一共有五张表
Student(学生表)
Address(地址表)
Classes(班级表)
Course(课程表)
Student_Course(学生-课程关联表)
关系结构:
Student与Address为一对一关系
Student与Classes为一对多关系
Student与Course为多对多关系
Address.hbm.xml文件如下:
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
<
hibernate-mapping
>
6
7
<!--
class 标签里写明Class文件的地址与相对应的表名
-->
8
<
class
9
name
="student.hibernate.Address"
10
table
="Address"
11
>
12
<!--
id 为主键
13
name 为Class(JavaBean) 中的一个属性
14
type 为Java类型
15
column 标识Class文件对应的数据表的主键
-->
16
<
id
17
name
="id"
18
type
="java.lang.String"
19
column
="id"
20
>
21
<!--
以下是生成主键的方式
22
class="foreign" 表示为以外键方式生成(foregin为系统自带的主键生成器),需要与one-to-one联合起来使用
23
如果这个生成器实例需要某些配置值或者初始化参数,用<param>元素来传递。
24
这里的属性 property 为外键生成所必须的参数?为one-one结构的第二个one的 name?
-->
25
<
generator
class
="foreign"
>
26
<
param
name
="property"
>
student
</
param
>
27
</
generator
>
28
</
id
>
29
<!--
property 定义 Class(JavaBean)的属性,name为属性名,column为对应的表的字段
-->
30
<
property
31
name
="city"
32
type
="java.lang.String"
33
column
="city"
34
not-null
="true"
35
length
="20"
36
/>
37
<
property
38
name
="street"
39
type
="java.lang.String"
40
column
="street"
41
length
="20"
42
/>
43
<
property
44
name
="doorplate"
45
type
="java.lang.Integer"
46
column
="doorplate"
47
length
="10"
48
/>
49
50
<!--
bi-directional one-to-one association to Student
-->
51
<!--
以下是与Student对象一对一关系的定义
52
name="student"为JavaBean中定义的Student类的属性
-->
53
<
one-to-one
54
name
="student"
55
class
="student.hibernate.Student"
56
outer-join
="auto"
57
constrained
="false"
58
/>
59
</
class
>
60
</
hibernate-mapping
>
61
Student.hbm.xml的代码:
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<
class
name
="student.hibernate.Student"
table
="Student"
>
<
id
name
="id"
type
="java.lang.String"
column
="id"
>
<
generator
class
="uuid.hex"
></
generator
>
</
id
>
<
property
name
="name"
type
="java.lang.String"
column
="name"
not-null
="true"
length
="20"
/>
<
property
name
="sex"
type
="java.lang.String"
column
="sex"
length
="10"
/>
<!--
bi-directional one-to-many association to StudentCourse
-->
<!--
key is foregin key of table,use the first "many"'s primary key
-->
<
set
name
="courses"
lazy
="true"
inverse
="true"
cascade
="none"
table
="student_course"
>
<
key
>
<
column
name
="studentId"
/>
</
key
>
<
many-to-many
class
="student.hibernate.Course"
column
="courseId"
>
</
many-to-many
>
</
set
>
<!--
many to one: column是以Classes表的主键作为此表的外键
-->
<
many-to-one
name
="classes"
column
="classesId"
/>
<!--
bi-directional one-to-one association to Address
-->
<
one-to-one
name
="address"
class
="student.hibernate.Address"
outer-join
="auto"
cascade
="all"
/>
</
class
>
</
hibernate-mapping
>
Classes.hbm.xml文件代码
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<
class
name
="student.hibernate.Classes"
table
="Classes"
>
<
id
name
="id"
type
="java.lang.Integer"
column
="id"
>
<!--
主键生成方式“increment”表示自动增加,用于 int ,long,short 类型
-->
<
generator
class
="increment"
/>
</
id
>
<
property
name
="name"
type
="java.lang.String"
column
="name"
length
="20"
/>
<!--
Associations
-->
<!--
bi-directional one-to-many association to Student
一对多的关系--用set(JavaBean中定义的集合类型)标签
name 为类属性
lazy 为延迟装载
inverse (可选——默认为false) 标记这个集合作为双向关联关系中的方向一端。
cascade 表示级联更新
-->
<
set
name
="students"
lazy
="true"
inverse
="true"
cascade
="none"
>
<!--
key 表示用本xml文件定义的表的主键作为 many(Student)端对应表的外键
-->
<
key
>
<
column
name
="classesId"
></
column
>
</
key
>
<
one-to-many
class
="student.hibernate.Student"
></
one-to-many
>
</
set
>
</
class
>
</
hibernate-mapping
>
Course.hbm.xml文件
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
<
hibernate-mapping
>
6
7
<
class
name
="student.hibernate.Course"
table
="Course"
>
8
<
id
name
="id"
type
="java.lang.Integer"
column
="id"
>
9
<
generator
class
="increment"
></
generator
>
10
</
id
>
11
12
<
property
name
="name"
type
="java.lang.String"
column
="name"
13
not-null
="true"
length
="20"
/>
14
15
<!--
bi-directional one-to-many association to StudentCourse
-->
16
<!--
多对多关系,这里用 set 标签
17
table 为多对多关系中的中间表
18
key 定义本xml文件定义的表的主键作为中间表的外键
-->
19
<
set
name
="students"
lazy
="true"
inverse
="true"
20
cascade
="none"
table
="student_course"
>
21
<
key
>
22
<
column
name
="courseId"
></
column
>
23
</
key
>
24
<!--
定义多对多关系中后一个 many 对应的Class
25
column 为以后一个many(Student)对应表的主键作为中间表的外键
-->
26
<
many-to-many
class
="student.hibernate.Student"
27
column
="studentId"
>
28
</
many-to-many
>
29
</
set
>
30
</
class
>
31
</
hibernate-mapping
>
32
posted on 2005-06-03 06:18
Sunarrow的Java日志
阅读(678)
评论(1)
编辑
收藏
所属分类:
Hibernate
Comments
#
re: Hibernate的映射
spiderman
非常好的总结,通过它我今天终于把一对一,一对多,多对一,多对多搞清楚了。以后就不用看着书上的例子配关系了。好开心呀。
Posted @ 2006-09-21 16:02
回复
更多评论
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理