Hibernate2 Reference Documentation
Version: 2.1.2
Table of Contents
Preface ................................................................................................................................................
1. Quickstart with Tomcat ................................................................................................................
1.1. Getting started with Hibernate ............................................................................................... 1
1.2. First persistent class .............................................................................................................. 3
1.3. Mapping the cat ................................................................................................................... 4
1.4. Playing with cats .................................................................................................................. 5
1.5. Finally ................................................................................................................................. 7
2. Architecture ..................................................................................................................................
2.1. Overview ............................................................................................................................. 8
2.2. Persistent Object Identity ...................................................................................................... 10
2.3. JMX Integration ................................................................................................................... 10
2.4. JCA Support ........................................................................................................................ 10
3. SessionFactory Configuration .......................................................................................................
3.1. Programmatic Configuration ................................................................................................. 11
3.2. Obtaining a SessionFactory ................................................................................................... 11
3.3. User provided JDBC connection ........................................................................................... 11
3.4. Hibernate provided JDBC connection .................................................................................... 12
3.5. Other properties ................................................................................................................... 13
3.5.1. SQL Dialects ............................................................................................................. 15
3.5.2. Outer Join Fetching ................................................................................................... 16
3.5.3. Binary Streams .......................................................................................................... 16
3.5.4. SQL Logging to Console ............................................................................................ 16
3.5.5. Custom ConnectionProvider ....................................................................................... 16
3.5.6. Common connection properties .................................................................................. 16
3.5.7. Custom CacheProvider .............................................................................................. 17
3.5.8. Transaction Strategy .................................................................................................. 17
3.5.9. JNDI-bound SessionFactory ....................................................................................... 18
3.5.10. Query Language Substitution ................................................................................... 18
3.6. XML Configuration File ....................................................................................................... 18
3.7. Logging ............................................................................................................................... 19
3.8. NamingStrategy ................................................................................................................... 19
4. Persistent Classes ..........................................................................................................................
4.1. Simple Example ................................................................................................................... 20
4.1.1. Declare accessors and mutators for persistent fields ..................................................... 21
4.1.2. Implement a default constructor .................................................................................. 21
4.1.3. Provide an identifier property (optional) ...................................................................... 21
4.1.4. Prefer non-final classes (optional) ............................................................................... 21
4.2. Inheritance ........................................................................................................................... 21
4.3. Persistent Lifecycle Callbacks ............................................................................................... 22
4.4. Validatable .......................................................................................................................... 22
4.5. XDoclet Example ................................................................................................................. 23
5. Basic O/R Mapping .......................................................................................................................
5.1. Mapping declaration ............................................................................................................. 25
5.1.1. Doctype .................................................................................................................... 25
5.1.2. hibernate-mapping ..................................................................................................... 25
5.1.3. class ......................................................................................................................... 26
5.1.4. id .............................................................................................................................. 28
5.1.4.1. generator ........................................................................................................ 28
Hibernate 2.1.2 ii
5.1.4.2. Hi/Lo Algorithm ............................................................................................. 29
5.1.4.3. UUID Algorithm ............................................................................................ 30
5.1.4.4. Identity Columns and Sequences ...................................................................... 30
5.1.4.5. Assigned Identifiers ........................................................................................ 30
5.1.5. composite-id ............................................................................................................. 30
5.1.6. discriminator ............................................................................................................. 31
5.1.7. version (optional) ...................................................................................................... 31
5.1.8. timestamp (optional) .................................................................................................. 32
5.1.9. property .................................................................................................................... 32
5.1.10. many-to-one ............................................................................................................ 33
5.1.11. one-to-one ............................................................................................................... 34
5.1.12. component, dynamic-component .............................................................................. 35
5.1.13. subclass .................................................................................................................. 36
5.1.14. joined-subclass ........................................................................................................ 36
5.1.15. map, set, list, bag ..................................................................................................... 37
5.1.16. import ..................................................................................................................... 37
5.2. Hibernate Types ................................................................................................................... 38
5.2.1. Entities and values ..................................................................................................... 38
5.2.2. Basic value types ....................................................................................................... 38
5.2.3. Persistent enum types ................................................................................................. 39
5.2.4. Custom value types .................................................................................................... 40
5.2.5. Any type mappings .................................................................................................... 40
5.3. SQL quoted identifiers .......................................................................................................... 41
5.4. Customizing the DDL ........................................................................................................... 41
5.5. Modular mapping files .......................................................................................................... 42
6. Collections .....................................................................................................................................
6.1. Persistent Collections ........................................................................................................... 43
6.2. Mapping a Collection ........................................................................................................... 43
6.3. Collections of Values and Many To Many Associations .......................................................... 45
6.4. One To Many Associations ................................................................................................... 46
6.5. Lazy Initialization ................................................................................................................ 47
6.6. Sorted Collections ................................................................................................................ 48
6.7. Other Ways To Sort a Collection ........................................................................................... 48
6.8. Garbage Collection ............................................................................................................... 49
6.9. Bidirectional Associations .................................................................................................... 49
6.10. Ternary Associations .......................................................................................................... 50
6.11. Heterogeneous Associations ................................................................................................ 50
6.12. Collection Example ............................................................................................................ 50
6.13. <idbag> ............................................................................................................................. 52
7. Components ..................................................................................................................................
7.1. As Dependent Objects .......................................................................................................... 53
7.2. In Collections ....................................................................................................................... 54
7.3. As a Map Index .................................................................................................................... 55
7.4. As Composite Identifiers ...................................................................................................... 55
7.5. Dynamic components ........................................................................................................... 57
8. Manipulating Persistent Data ........................................................................................................
8.1. Creating a persistent object ................................................................................................... 58
8.2. Loading an object ................................................................................................................. 58
8.3. Querying ............................................................................................................................. 59
8.3.1. Scalar queries ............................................................................................................ 60
8.3.2. The Query interface ................................................................................................... 61
8.3.3. Scrollable iteration .................................................................................................... 62
HIBERNATE - Relational Persistence for Idiomatic Java
Hibernate 2.1.2 iii
8.3.4. Filtering collections ................................................................................................... 62
8.3.5. Criteria queries .......................................................................................................... 63
8.3.6. Queries in native SQL ................................................................................................ 63
8.4. Updating objects saved or loaded in the current session .......................................................... 63
8.5. Updating objects saved or loaded in a previous session ........................................................... 63
8.6. Reassociating objects saved or loaded in a previous session .................................................... 65
8.7. Deleting persistent objects .................................................................................................... 65
8.8. Graphs of objects ................................................................................................................. 65
8.8.1. Lifecycle objects ....................................................................................................... 66
8.8.2. Persistence by Reachability ........................................................................................ 66
8.9. Flushing .............................................................................................................................. 66
8.10. Ending a Session ................................................................................................................ 67
8.10.1. Flushing the session ................................................................................................. 67
8.10.2. Committing the transaction ....................................................................................... 67
8.10.3. Closing the session .................................................................................................. 68
8.10.4. Exception handling .................................................................................................. 68
8.11. Interceptors ........................................................................................................................ 69
8.12. Metadata API ..................................................................................................................... 70
9. Parent/Child Relationships ............................................................................................................
9.1. A note about collections ........................................................................................................ 72
9.2. Bidirectional one to many ..................................................................................................... 72
9.3. Cascades .............................................................................................................................. 73
9.4. Using cascading update() ...................................................................................................... 74
9.5. Conclusion ........................................................................................................................... 76
10. Hibernate Query Language .........................................................................................................
10.1. Case Sensitivity .................................................................................................................. 77
10.2. The from clause .................................................................................................................. 77
10.3. Associations and joins ........................................................................................................ 77
10.4. The select clause ................................................................................................................ 78
10.5. Aggregate functions ............................................................................................................ 79
10.6. polymorphism .................................................................................................................... 79
10.7. The where clause ................................................................................................................ 80
10.8. Expressions ........................................................................................................................ 81
10.9. The order by clause ............................................................................................................ 83
10.10. The group by clause .......................................................................................................... 83
10.11. Subqueries ....................................................................................................................... 84
10.12. Examples ......................................................................................................................... 84
10.13. Tips & Tricks ................................................................................................................... 86
11. A Worked Example .....................................................................................................................
11.1. Persistent Classes ............................................................................................................... 88
11.2. Hibernate Mappings ........................................................................................................... 89
11.3. Hibernate Code .................................................................................................................. 90
12. Improving Performance ..............................................................................................................
12.1. Proxies for Lazy Initialization ............................................................................................. 94
12.2. The Second Level Cache ..................................................................................................... 95
12.2.1. Mapping ................................................................................................................. 96
12.2.2. read only ................................................................................................................. 96
12.2.3. read / write .............................................................................................................. 96
12.2.4. nonstrict read / write ................................................................................................ 97
12.2.5. transactional ............................................................................................................ 97
12.3. Managing the Session Cache ............................................................................................... 97
12.4. The Query Cache ................................................................................................................ 98
HIBERNATE - Relational Persistence for Idiomatic Java
Hibernate 2.1.2 iv
13. Understanding Collection Performance .......................................................................................
13.1. Taxonomy .......................................................................................................................... 99
13.2. Lists, maps and sets are the most efficient collections to update ............................................. 99
13.3. Bags and lists are the most efficient inverse collections ......................................................... 100
13.4. One shot delete ................................................................................................................... 100
14. Criteria Queries ..........................................................................................................................
14.1. Creating a Criteria instance ................................................................................................. 102
14.2. Narrowing the result set ...................................................................................................... 102
14.3. Ordering the results ............................................................................................................ 103
14.4. Associations ....................................................................................................................... 103
14.5. Dynamic association fetching .............................................................................................. 103
14.6. Example queries ................................................................................................................. 104
15. SQL Queries ................................................................................................................................
15.1. Creating a SQL based Query ............................................................................................... 105
15.2. Alias and property references .............................................................................................. 105
15.3. Named SQL queries ........................................................................................................... 105
16. Inheritance Mappings .................................................................................................................
16.1. The Three Strategies ........................................................................................................... 107
16.2. Limitations ......................................................................................................................... 109
17. Transactions And Concurrency ...................................................................................................
17.1. Configurations, Sessions and Factories ................................................................................ 111
17.2. Threads and connections ..................................................................................................... 111
17.3. Optimistic Locking / Versioning .......................................................................................... 111
17.3.1. Long session with automatic versioning .................................................................... 112
17.3.2. Many sessions with automatic versioning .................................................................. 112
17.3.3. Application version checking ................................................................................... 112
17.4. Session disconnection ......................................................................................................... 112
17.5. Pessimistic Locking ............................................................................................................ 114
18. Mapping Examples ......................................................................................................................
18.1. Employer/Employee ........................................................................................................... 115
18.2. Author/Work ...................................................................................................................... 116
18.3. Customer/Order/Product ..................................................................................................... 118
19. Toolset Guide ..............................................................................................................................
19.1. Schema Generation ............................................................................................................. 121
19.1.1. Customizing the schema ........................................................................................... 121
19.1.2. Running the tool ...................................................................................................... 122
19.1.3. Properties ................................................................................................................ 123
19.1.4. Using Ant ............................................................................................................... 123
19.1.5. Incremental schema updates ..................................................................................... 124
19.1.6. Using Ant for incremental schema updates ................................................................ 124
19.2. Code Generation ................................................................................................................ 124
19.2.1. The config file (optional) ......................................................................................... 125
19.2.2. The meta attribute .................................................................................................... 125
19.2.3. Basic finder generator .............................................................................................. 127
19.2.4. Velocity based renderer/generator ............................................................................. 128
19.3. Mapping File Generation .................................................................................................... 128
19.3.1. Running the tool ...................................................................................................... 129
20. Best Practices ..............................................................................................................................
HIBERNATE - Relational Persistence for Idiomatic Java
Hibernate 2.1.2 v
- 1
- 2
前往页