HIBERNATE - Relational
Persistence for Idiomatic Java
1
Hibernate Reference
Documentation
5.3.7.Final
由 Gavin King、Christian Bauer、Max Rydahl
Andersen、Emmanuel Bernard、Steve Ebersole和Hardy Ferentschik
and thanks to James Cobb (Graphic Design)、Cheyenne
Weaver (Graphic Design)和Cao RedSaga Xiaogang
iii
前言 ............................................................................ xi
1. 教程 ......................................................................... 1
1.1. 第一部分 - 第一个 Hibernate 应用程序 ................................. 1
1.1.1. 设置 ............................................................ 1
1.1.2. 第一个 class ................................................... 3
1.1.3. 映射文件 ........................................................ 4
1.1.4. Hibernate 配置 .................................................. 7
1.1.5. 用 Maven 构建 .................................................. 9
1.1.6. 启动和辅助类 .................................................... 9
1.1.7. 加载并存储对象 ................................................. 10
1.2. 第二部分 - 关联映射 ................................................. 13
1.2.1. 映射 Person 类 ................................................ 13
1.2.2. 单向 Set-based 的关联 ......................................... 14
1.2.3. 使关联工作 ..................................................... 15
1.2.4. 值类型的集合 ................................................... 17
1.2.5. 双向关联 ....................................................... 18
1.2.6. 使双向连起来 ................................................... 19
1.3. 第三部分 - EventManager web 应用程序 ................................. 20
1.3.1. 编写基本的 servlet ............................................. 20
1.3.2. 处理与渲染 ..................................................... 21
1.3.3. 部署与测试 ..................................................... 23
1.4. 总结 ................................................................. 24
2. 体系结构(Architecture) ..................................................... 25
2.1. 概况(Overview) ...................................................... 25
2.1.1. Minimal architecture ........................................... 25
2.1.2. Comprehensive architecture ..................................... 26
2.1.3. Basic APIs .................................................... 27
2.2. JMX 整合 ............................................................. 28
2.3. 上下文相关的会话(Contextual Session) ................................ 28
3. 配置 ........................................................................ 31
3.1. 可编程的配置方式 ...................................................... 31
3.2. 获得 SessionFactory .................................................. 32
3.3. JDBC 连接 ............................................................ 32
3.4. 可选的配置属性 ........................................................ 34
3.4.1. SQL 方言 ...................................................... 41
3.4.2. 外连接抓取(Outer Join Fetching) .............................. 42
3.4.3. 二进制流(Binary Streams) ..................................... 42
3.4.4. 二级缓存与查询缓存 ............................................. 42
3.4.5. 查询语言中的替换 ............................................... 42
3.4.6. Hibernate 的统计(statistics)机制 ............................. 43
3.5. 日志 ................................................................. 43
3.6. 实现 NamingStrategy .................................................. 43
3.7. Implementing a PersisterClassProvider ................................. 44
3.8. XML 配置文件 ......................................................... 44
HIBERNATE - Relational Persis...
iv
3.9. Java EE Application Server integration ............................... 46
3.9.1. 事务策略配置 ................................................... 46
3.9.2. JNDI 绑定的 SessionFactory ..................................... 47
3.9.3. 在 JTA 环境下使用 Current Session context(当前 session 上下文)
管理 .................................................................. 48
3.9.4. JMX 部署 ...................................................... 48
4. 持久化类(Persistent Classes) .............................................. 51
4.1. 一个简单的 POJO 例子 ................................................. 51
4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) ............. 52
4.1.2. Provide an identifier property ................................. 53
4.1.3. Prefer non-final classes (semi-optional) ....................... 53
4.1.4. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators)
(可选) .............................................................. 54
4.2. 实现继承(Inheritance) ............................................... 54
4.3. 实现 equals() 和 hashCode() 方法: ................................... 55
4.4. 动态模型(Dynamic models) ............................................ 56
4.5. 元组片断映射(Tuplizers) ............................................. 58
4.6. EntityNameResolvers ................................................... 59
5. 对象/关系数据库映射基础(Basic O/R Mapping) ................................. 63
5.1. 映射定义(Mapping declaration) ....................................... 63
5.1.1. Entity ......................................................... 66
5.1.2. Identifiers .................................................... 71
5.1.3. Optimistic locking properties (optional) ....................... 89
5.1.4. Property ....................................................... 92
5.1.5. Embedded objects (aka components) ............................. 101
5.1.6. Inheritance strategy .......................................... 104
5.1.7. Mapping one to one and one to many associations ............... 115
5.1.8. 自然 ID(natural-id) ......................................... 123
5.1.9. Any ........................................................... 124
5.1.10. 属性(Properties) ........................................... 126
5.1.11. Some hbm.xml specificities ................................... 128
5.2. Hibernate 的类型 .................................................... 131
5.2.1. 实体(Entities)和值(values) ................................ 131
5.2.2. 基本值类型 .................................................... 132
5.2.3. 自定义值类型 .................................................. 134
5.3. 多次映射同一个类 ..................................................... 135
5.4. SQL 中引号包围的标识符 ............................................... 135
5.5. 数据库生成属性(Generated Properties) ............................... 136
5.6. Column transformers: read and write expressions ...................... 136
5.7. 辅助数据库对象(Auxiliary Database Objects) ......................... 137
6. Types ...................................................................... 139
6.1. Value types ......................................................... 139
6.1.1. Basic value types ............................................ 139
6.1.2. Composite types ............................................... 146
v
6.1.3. Collection types .............................................. 146
6.2. Entity types ........................................................ 146
6.3. Significance of type categories ..................................... 147
6.4. Custom types ........................................................ 147
6.4.1. Custom types using org.hibernate.type.Type .................... 147
6.4.2. Custom types using org.hibernate.usertype.UserType ............ 149
6.4.3. Custom types using org.hibernate.usertype.CompositeUserType .... 150
6.5. Type registry ....................................................... 151
7. 集合映射(Collection mappings) ............................................ 153
7.1. 持久化集合类(Persistent collections) ............................... 153
7.2. How to map collections .............................................. 154
7.2.1. 集合外键(Collection foreign keys) ........................... 157
7.2.2. 索引集合类(Indexed collections) ............................. 158
7.2.3. Collections of basic types and embeddable objects ............. 164
7.3. 高级集合映射(Advanced collection mappings) ......................... 166
7.3.1. 有序集合(Sorted collections) ................................ 166
7.3.2. 双向关联(Bidirectional associations) ........................ 167
7.3.3. 双向关联,涉及有序集合类 ...................................... 172
7.3.4. 三重关联(Ternary associations) .............................. 173
7.3.5. Using an <idbag> ............................................. 174
7.4. 集合例子(Collection example) ....................................... 174
8. 关联关系映射 ............................................................... 181
8.1. 介绍 ................................................................ 181
8.2. 单向关联(Unidirectional associations) .............................. 181
8.2.1. 多对一(many-to-one) ......................................... 181
8.2.2. 一对一(One-to-one) .......................................... 181
8.2.3. 一对多(one-to-many) ......................................... 182
8.3. 使用连接表的单向关联(Unidirectional associations with join tables) .. 183
8.3.1. 一对多(one-to-many) ......................................... 183
8.3.2. 多对一(many-to-one) ......................................... 184
8.3.3. 一对一(One-to-one) .......................................... 184
8.3.4. 多对多(many-to-many) ........................................ 185
8.4. 双向关联(Bidirectional associations) ............................... 186
8.4.1. 一对多(one to many)/多对一(many to one) .................... 186
8.4.2. 一对一(One-to-one) .......................................... 187
8.5. 使用连接表的双向关联(Bidirectional associations with join tables) ... 188
8.5.1. 一对多(one to many)/多对一(many to one) .................... 188
8.5.2. 一对一(one to one) ......................................... 189
8.5.3. 多对多(many-to-many) ........................................ 190
8.6. 更复杂的关联映射 ..................................................... 190
9. 组件(Component)映射 ....................................................... 193
9.1. 依赖对象(Dependent objects) ........................................ 193
9.2. 在集合中出现的依赖对象(Collections of dependent objects) ........... 195
9.3. 组件作为 Map 的索引(Components as Map indices ) .................... 196