Author(s): Christian Bauer; Gavin King
Publisher: Manning
Year: 2005
Language: English
Pages: 432
City: Greenwich, CT
contents......Page 5
foreword......Page 11
preface......Page 13
acknowledgments......Page 15
Roadmap......Page 16
Code conventions and downloads......Page 18
About the authors......Page 19
about Hibernate3 and EJB 3......Page 20
author online......Page 21
About the cover illustration......Page 22
Understanding object/relational persistence......Page 25
1.1.1 Relational databases......Page 27
1.1.2 Understanding SQL......Page 28
1.1.4 Persistence in object-oriented applications......Page 29
1.2 The paradigm mismatch......Page 31
1.2.1 The problem of granularity......Page 33
1.2.2 The problem of subtypes......Page 34
1.2.3 The problem of identity......Page 35
1.2.4 Problems relating to associations......Page 37
1.2.5 The problem of object graph navigation......Page 38
1.2.6 The cost of the mismatch......Page 39
1.3 Persistence layers and alternatives......Page 40
1.3.1 Layered architecture......Page 41
1.3.2 Hand-coding a persistence layer with SQL/JDBC......Page 42
1.3.3 Using serialization......Page 43
1.3.4 Considering EJB entity beans......Page 44
1.3.5 Object-oriented database systems......Page 45
1.4 Object/relational mapping......Page 46
1.4.1 What is ORM?......Page 47
1.4.2 Generic ORM problems......Page 49
1.4.3 Why ORM?......Page 50
1.5 Summary......Page 53
Introducing and integrating Hibernate......Page 54
2.1 “Hello World” with Hibernate......Page 55
2.2 Understanding the architecture......Page 60
2.2.1 The core interfaces......Page 62
2.2.3 Types......Page 64
2.3 Basic configuration......Page 65
2.3.1 Creating a SessionFactory......Page 66
2.3.2 Configuration in non-managed environments......Page 69
2.3.3 Configuration in managed environments......Page 72
2.4.1 Using XML-based configuration......Page 75
2.4.2 JNDI-bound SessionFactory......Page 77
2.4.3 Logging......Page 78
2.4.4 Java Management Extensions (JMX)......Page 79
2.5 Summary......Page 82
Mapping persistent classes......Page 83
3.1 The CaveatEmptor application......Page 84
3.1.2 The CaveatEmptor domain model......Page 85
3.2.1 Addressing leakage of concerns......Page 88
3.2.2 Transparent and automated persistence......Page 89
3.2.3 Writing POJOs......Page 91
3.2.4 Implementing POJO associations......Page 93
3.2.5 Adding logic to accessor methods......Page 97
3.3.1 Metadata in XML......Page 99
3.3.2 Basic property and class mappings......Page 102
3.3.3 Attribute-oriented programming......Page 108
3.3.4 Manipulating metadata at runtime......Page 110
3.4.1 Identity versus equality......Page 111
3.4.2 Database identity with Hibernate......Page 112
3.4.3 Choosing primary keys......Page 114
3.5 Fine-grained object models......Page 116
3.5.2 Using components......Page 117
3.6.1 Table per concrete class......Page 121
3.6.2 Table per class hierarchy......Page 123
3.6.3 Table per subclass......Page 125
3.6.4 Choosing a strategy......Page 128
3.7 Introducing associations......Page 129
3.7.2 Multiplicity......Page 130
3.7.3 The simplest possible association......Page 131
3.7.4 Making the association bidirectional......Page 132
3.7.5 A parent/child relationship......Page 135
3.8 Summary......Page 136
Working with persistent objects......Page 138
4.1 The persistence lifecycle......Page 139
4.1.1 Transient objects......Page 140
4.1.2 Persistent objects......Page 141
4.1.3 Detached objects......Page 142
4.1.4 The scope of object identity......Page 143
4.1.5 Outside the identity scope......Page 145
4.1.6 Implementing equals() and hashCode()......Page 146
4.2.1 Making an object persistent......Page 150
4.2.2 Updating the persistent state of a detached instance......Page 151
4.2.5 Making a persistent object transient......Page 153
4.2.6 Making a detached object transient......Page 154
4.3.1 Persistence by reachability......Page 155
4.3.2 Cascading persistence with Hibernate......Page 157
4.3.3 Managing auction categories......Page 158
4.3.4 Distinguishing between transient and detached instances......Page 162
4.4 Retrieving objects......Page 163
4.4.1 Retrieving objects by identifier......Page 164
4.4.2 Introducing HQL......Page 165
4.4.3 Query by criteria......Page 166
4.4.5 Fetching strategies......Page 167
4.4.6 Selecting a fetching strategy in mappings......Page 170
4.4.7 Tuning object retrieval......Page 175
4.5 Summary......Page 176
Transactions, concurrency, and caching......Page 178
5.1 Understanding database transactions......Page 180
5.1.1 JDBC and JTA transactions......Page 181
5.1.2 The Hibernate Transaction API......Page 182
5.1.3 Flushing the Session......Page 184
5.1.4 Understanding isolation levels......Page 185
5.1.5 Choosing an isolation level......Page 187
5.1.7 Using pessimistic locking......Page 189
5.2 Working with application transactions......Page 192
5.2.1 Using managed versioning......Page 193
5.2.2 Granularity of a Session......Page 196
5.2.3 Other ways to implement optimistic locking......Page 198
5.3 Caching theory and practice......Page 199
5.3.1 Caching strategies and scopes......Page 200
5.3.2 The Hibernate cache architecture......Page 203
5.3.3 Caching in practice......Page 209
5.4 Summary......Page 218
Advanced mapping concepts......Page 219
6.1 Understanding the Hibernate type system......Page 220
6.1.1 Built-in mapping types......Page 222
6.1.2 Using mapping types......Page 224
6.2.1 Sets, bags, lists, and maps......Page 235
6.3.1 One-to-one associations......Page 244
6.3.2 Many-to-many associations......Page 249
6.4.1 Polymorphic many-to-one associations......Page 258
6.4.2 Polymorphic collections......Page 260
6.4.3 Polymorphic associations and table-per-concrete-class......Page 261
6.5 Summary......Page 263
Retrieving objects efficiently......Page 265
7.1.1 The query interfaces......Page 267
7.1.2 Binding parameters......Page 269
7.1.3 Using named queries......Page 273
7.2.1 The simplest query......Page 274
7.2.3 Polymorphic queries......Page 275
7.2.4 Restriction......Page 276
7.2.5 Comparison operators......Page 277
7.2.6 String matching......Page 279
7.2.7 Logical operators......Page 280
7.2.8 Ordering query results......Page 281
7.3 Joining associations......Page 282
7.3.1 Hibernate join options......Page 283
7.3.2 Fetching associations......Page 284
7.3.3 Using aliases with joins......Page 286
7.3.4 Using implicit joins......Page 289
7.3.5 Theta-style joins......Page 291
7.3.6 Comparing identifiers......Page 292
7.4 Writing report queries......Page 293
7.4.1 Projection......Page 294
7.4.2 Using aggregation......Page 296
7.4.3 Grouping......Page 297
7.4.4 Restricting groups with having......Page 298
7.4.5 Improving performance with report queries......Page 299
7.5.1 Dynamic queries......Page 300
7.5.2 Collection filters......Page 303
7.5.3 Subqueries......Page 305
7.5.4 Native SQL queries......Page 307
7.6.1 Solving the n+1 selects problem......Page 310
7.6.2 Using iterate() queries......Page 313
7.6.3 Caching queries......Page 314
7.7 Summary......Page 316
Writing Hibernate applications......Page 318
8.1 Designing layered applications......Page 319
8.1.1 Using Hibernate in a servlet engine......Page 320
8.1.2 Using Hibernate in an EJB container......Page 335
8.2 Implementing application transactions......Page 344
8.2.1 Approving a new auction......Page 345
8.2.2 Doing it the hard way......Page 346
8.2.3 Using detached persistent objects......Page 348
8.2.4 Using a long session......Page 349
8.2.5 Choosing an approach to application transactions......Page 353
8.3.1 Legacy schemas and composite keys......Page 354
8.3.2 Audit logging......Page 364
8.4 Summary......Page 371
Using the toolset......Page 372
9.1 Development processes......Page 373
9.1.4 Meet in the middle......Page 374
9.2 Automatic schema generation......Page 375
9.2.1 Preparing the mapping metadata......Page 376
9.2.2 Creating the schema......Page 379
9.2.3 Updating the schema......Page 381
9.3.1 Adding meta-attributes......Page 382
9.3.2 Generating finders......Page 384
9.3.3 Configuring hbm2java......Page 386
9.3.4 Running hbm2java......Page 387
9.4.1 Starting Middlegen......Page 388
9.4.2 Restricting tables and relationships......Page 390
9.4.3 Customizing the metadata generation......Page 392
9.4.4 Generating hbm2java and XDoclet metadata......Page 394
9.5.1 Setting value type attributes......Page 396
9.5.2 Mapping entity associations......Page 398
9.5.3 Running XDoclet......Page 399
9.6 Summary......Page 400
SQL fundamentals......Page 402
ORM implementation strategies......Page 406
B.1 Properties or fields?......Page 407
B.2.1 Inheritance from generated code......Page 408
B.2.4 Reflection......Page 409
B.2.5 Runtime bytecode generation......Page 410
B.2.6 “Generic” objects......Page 411
Back in the real world......Page 412
C.1 The strange copy......Page 413
C.3 We don’t need primary keys......Page 414
C.5 Dynamically unsafe......Page 415
C.6 To synchronize or not?......Page 416
C.7 Really fat client......Page 417
C.8 Resuming Hibernate......Page 418
references......Page 419
index......Page 421