HIBERNATE - Relational
Persistence for Idiomatic Java
1
Hibernate Reference
Documentation
3.5.5-Final
由 Gavin King、Christian Bauer、Max Rydahl Andersen、Emmanuel Bernard和Steve Ebersole
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. 部署与测试 ..................................................... 24
1.4. 总结 ................................................................. 25
2. 体系结构(Architecture) ..................................................... 27
2.1. 概况(Overview) ...................................................... 27
2.2. 实例状态 .............................................................. 29
2.3. JMX 整合 ............................................................. 30
2.4. 对 JCA 的支持 ........................................................ 30
2.5. 上下文相关的会话(Contextual Session) ................................ 30
3. 配置 ........................................................................ 33
3.1. 可编程的配置方式 ...................................................... 33
3.2. 获得 SessionFactory .................................................. 34
3.3. JDBC 连接 ............................................................ 34
3.4. 可选的配置属性 ........................................................ 36
3.4.1. SQL 方言 ...................................................... 42
3.4.2. 外连接抓取(Outer Join Fetching) .............................. 43
3.4.3. 二进制流(Binary Streams) ..................................... 43
3.4.4. 二级缓存与查询缓存 ............................................. 43
3.4.5. 查询语言中的替换 ............................................... 43
3.4.6. Hibernate 的统计(statistics)机制 ............................. 43
3.5. 日志 ................................................................. 44
3.6. 实现 NamingStrategy .................................................. 44
3.7. XML 配置文件 ......................................................... 45
3.8. J2EE 应用程序服务器的集成 ............................................. 46
3.8.1. 事务策略配置 ................................................... 46
HIBERNATE - Relational Persis...
iv
3.8.2. JNDI 绑定的 SessionFactory ..................................... 47
3.8.3. 在 JTA 环境下使用 Current Session context(当前 session 上下文)
管理 .................................................................. 48
3.8.4. JMX 部署 ...................................................... 48
4. 持久化类(Persistent Classes) .............................................. 51
4.1. 一个简单的 POJO 例子 ................................................. 51
4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) ............. 52
4.1.2. 提供一个标识属性(identifier property)(可选) ................. 52
4.1.3. 使用非final的类(可选) ........................................ 53
4.1.4. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators)
(可选) .............................................................. 53
4.2. 实现继承(Inheritance) ............................................... 53
4.3. 实现 equals() 和 hashCode() 方法: ................................... 54
4.4. 动态模型(Dynamic models) ............................................ 55
4.5. 元组片断映射(Tuplizers) ............................................. 57
4.6. EntityNameResolvers ................................................... 58
5. 对象/关系数据库映射基础(Basic O/R Mapping) ................................. 61
5.1. 映射定义(Mapping declaration) ....................................... 61
5.1.1. Doctype ........................................................ 62
5.1.2. Hibernate-mapping .............................................. 63
5.1.3. 类 ............................................................. 64
5.1.4. id ............................................................. 67
5.1.5. 增强的标识符生成器 ............................................. 71
5.1.6. 标识符生成器的优化 ............................................. 72
5.1.7. composite-id ................................................... 72
5.1.8. 鉴别器(discriminator) ........................................ 73
5.1.9. 版本(version)(可选) ........................................ 74
5.1.10. timestamp(可选) .............................................. 75
5.1.11. Property ...................................................... 76
5.1.12. 多对一(many-to-one) ......................................... 78
5.1.13. 一对一 ........................................................ 80
5.1.14. 自然 ID(natural-id) ......................................... 82
5.1.15. 组件(component)和动态组件(dynamic-component) ............... 82
5.1.16. 属性(Properties) ............................................ 83
5.1.17. 子类(subclass) .............................................. 84
5.1.18. 连接的子类(joined-subclass) ................................. 85
5.1.19. 联合子类(union-subclass) .................................... 86
5.1.20. 连接(join) .................................................. 87
5.1.21. Key ........................................................... 88
5.1.22. 字段和规则元素(column and formula elements) ................. 89
5.1.23. 引用(import) ................................................ 90
5.1.24. Any ........................................................... 90
5.2. Hibernate 的类型 ..................................................... 91
5.2.1. 实体(Entities)和值(values) ................................. 91
v
5.2.2. 基本值类型 ..................................................... 92
5.2.3. 自定义值类型 ................................................... 93
5.3. 多次映射同一个类 ...................................................... 94
5.4. SQL 中引号包围的标识符 ............................................... 95
5.5. 其他元数据(Metadata) ................................................ 95
5.5.1. 使用 XDoclet 标记 ............................................. 95
5.5.2. 使用 JDK 5.0 的注解(Annotation) .............................. 97
5.6. 数据库生成属性(Generated Properties) ................................ 98
5.7. 字段的读写表达式 ...................................................... 98
5.8. 辅助数据库对象(Auxiliary Database Objects) .......................... 99
6. 集合映射(Collection mappings) ............................................ 101
6.1. 持久化集合类(Persistent collections) ............................... 101
6.2. 集合映射( Collection mappings ) ................................... 102
6.2.1. 集合外键(Collection foreign keys) ........................... 103
6.2.2. 集合元素(Collection elements) ............................... 104
6.2.3. 索引集合类(Indexed collections) ............................. 104
6.2.4. 值集合于多对多关联(Collections of values and many-to-many
associations) ....................................................... 105
6.2.5. 一对多关联(One-to-many Associations) ........................ 107
6.3. 高级集合映射(Advanced collection mappings) ......................... 108
6.3.1. 有序集合(Sorted collections) ................................ 108
6.3.2. 双向关联(Bidirectional associations) ........................ 109
6.3.3. 双向关联,涉及有序集合类 ...................................... 111
6.3.4. 三重关联(Ternary associations) .............................. 112
6.3.5. Using an <idbag> ............................................. 112
6.4. 集合例子(Collection example) ....................................... 113
7. 关联关系映射 ............................................................... 117
7.1. 介绍 ................................................................ 117
7.2. 单向关联(Unidirectional associations) .............................. 117
7.2.1. 多对一(many-to-one) ......................................... 117
7.2.2. 一对一(One-to-one) .......................................... 117
7.2.3. 一对多(one-to-many) ......................................... 118
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) .. 119
7.3.1. 一对多(one-to-many) ......................................... 119
7.3.2. 多对一(many-to-one) ......................................... 120
7.3.3. 一对一(One-to-one) .......................................... 120
7.3.4. 多对多(many-to-many) ........................................ 121
7.4. 双向关联(Bidirectional associations) ............................... 122
7.4.1. 一对多(one to many)/多对一(many to one) .................... 122
7.4.2. 一对一(One-to-one) .......................................... 123
7.5. 使用连接表的双向关联(Bidirectional associations with join tables) ... 124
7.5.1. 一对多(one to many)/多对一(many to one) .................... 124
7.5.2. 一对一(one to one) ......................................... 125
7.5.3. 多对多(many-to-many) ........................................ 126