Hibernate in Action
Christian Bauer and Gavin King
  • August 2004
  • ISBN 9781932394153
  • 400 pages
  • printed in black & white

2005 Best Java Book!

Readers' Choice Awards, Java Developer's Journal


Java Persistence with Hibernate, Second Edition is now available. An eBook of the previous edition is included at no additional cost when you buy the revised edition!

Hibernate in Action carefully explains the concepts you need, then gets you going. It builds on a single example to show you how to use Hibernate in practice, how to deal with concurrency and transactions, how to efficiently retrieve objects and use caching.

The authors created Hibernate and they field questions from the Hibernate community every day - they know how to make Hibernate sing. Knowledge and insight seep out of every pore of this book.

Table of Contents detailed table of contents




about this book

about Hibernate3 and EJB 3

author online

about the title and cover

1. Understanding object/relational persistence

1.1. What is persistence?

1.1.1. Relational databases

1.1.2. Understanding SQL

1.1.3. Using SQL in Java

1.1.4. Persistence in object-oriented applications

1.2. The paradigm mismatch

1.2.1. The problem of granularity

1.2.2. The problem of subtypes

1.2.3. The problem of identity

1.2.4. Problems relating to associations

1.2.5. The problem of object graph navigation

1.2.6. The cost of the mismatch

1.3. Persistence layers and alternatives

1.3.1. Layered architecture

1.3.2. Hand-coding a persistence layer with SQL/JDBC

1.3.3. Using serialization

1.3.4. Considering EJB entity beans

1.3.5. Object-oriented database systems

1.3.6. Other options

1.4. Object/relational mapping

1.4.1. What is ORM?

1.4.2. Generic ORM problems

1.4.3. Why ORM?

1.5. Summary

2. Introducing and integrating Hibernate

2.1. "Hello World" with Hibernate

2.2. Understanding the architecture

2.2.1. The core interfaces

2.2.2. Callback interfaces

2.2.3. Types

2.2.4. Extension interfaces

2.3. Basic configuration

2.3.1. Creating a SessionFactory

2.3.2. Configuration in non-managed environments

2.3.3. Configuration in managed environments

2.4. Advanced configuration settings

2.4.1. Using XML-based configuration

2.4.2. JNDI-bound SessionFactory

2.4.3. Logging

2.4.4. Java Management Extensions (JMX)

2.5. Summary

3. Mapping persistent classes

3.1. The CaveatEmptor application

3.1.1. Analyzing the business domain

3.1.2. The CaveatEmptor domain model

3.2. Implementing the domain model

3.2.1. Addressing leakage of concerns

3.2.2. Transparent and automated persistence

3.2.3. Writing POJOs

3.2.4. Implementing POJO associations

3.2.5. Adding logic to accessor methods

3.3. Defining the mapping metadata

3.3.1. Metadata in XML

3.3.2. Basic property and class mappings

3.3.3. Attribute-oriented programming

3.3.4. Manipulating metadata at runtime

3.4. Understanding object identity

3.4.1. Identity versus equality

3.4.2. Database identity with Hibernate

3.4.3. Choosing primary keys

3.5. Fine-grained object models

3.5.1. Entity and value types

3.5.2. Using components

3.6. Mapping class inheritance

3.6.1. Table per concrete class

3.6.2. Table per class hierarchy

3.6.3. Table per subclass

3.6.4. Choosing a strategy

3.7. Introducing associations

3.7.1. Managed associations?

3.7.2. Multiplicity

3.7.3. The simplest possible association

3.7.4. Making the association bidirectional

3.7.5. A parent/child relationship

3.8. Summary

4. Working with persistent objects

4.1. The persistence lifecycle

4.1.1. Transient objects

4.1.2. Persistent objects

4.1.3. Detached objects

4.1.4. The scope of object identity

4.1.5. Outside the identity scope

4.1.6. Implementing equals() and hashCode()

4.2. The persistence manager

4.2.1. Making an object persistent

4.2.2. Updating the persistent state of a detached instance

4.2.3. Retrieving a persistent object

4.2.4. Updating a persistent object

4.2.5. Making a persistent object transient

4.2.6. Making a detached object transient

4.3. Using transitive persistence in Hibernate

4.3.1. Persistence by reachability

4.3.2. Cascading persistence with Hibernate

4.3.3. Managing auction categories

4.3.4. Distinguishing between transient and detached instances

4.4. Retrieving objects

4.4.1. Retrieving objects by identifier

4.4.2. Introducing HQL

4.4.3. Query by criteria

4.4.4. Query by example

4.4.5. Fetching strategies

4.4.6. Selecting a fetching strategy in mappings

4.4.7. Tuning object retrieval

4.5. Summary

5. Transactions, concurrency, and caching

5.1. Transactions, concurrency, and caching

5.2. Understanding database transactions

5.2.1. JDBC and JTA transactions

5.2.2. The Hibernate Transaction API

5.2.3. Flushing the Session

5.2.4. Understanding isolation levels

5.2.5. Choosing an isolation level

5.2.6. Setting an isolation level

5.2.7. Using pessimistic locking

5.3. Working with application transactions

5.3.1. Using managed versioning

5.3.2. Granularity of a Session

5.3.3. Other ways to implement optimistic locking

5.4. Caching theory and practice

5.4.1. Caching strategies and scopes

5.4.2. The Hibernate cache architecture

5.4.3. Caching in practice

5.5. Summary

6. Advanced mapping concepts

6.1. Understanding the Hibernate type system

6.1.1. Built-in mapping types

6.1.2. Using mapping types

6.2. Mapping collections of value types

6.2.1. Sets, bags, lists, and maps

6.3. Mapping entity associations

6.3.1. One-to-one associations

6.3.2. Many-to-many associations

6.4. Mapping polymorphic associations

6.4.1. Polymorphic many-to-one associations

6.4.2. Polymorphic collections

6.4.3. Polymorphic associations and table-per-concrete-class

6.5. Summary

7. Retrieving objects efficiently

7.1. Executing queries

7.1.1. The query interfaces

7.1.2. Binding parameters

7.1.3. Using named queries

7.2. Basic queries for objects

7.2.1. The simplest query

7.2.2. Using aliases

7.2.3. Polymorphic queries

7.2.4. Restriction

7.2.5. Comparison operators

7.2.6. String matching

7.2.7. Logical operators

7.2.8. Ordering query results

7.3. Joining associations

7.3.1. Hibernate join options

7.3.2. Fetching associations

7.3.3. Using aliases with joins

7.3.4. Using implicit joins

7.3.5. Theta-style joins

7.3.6. Comparing identifiers

7.4. Writing report queries

7.4.1. Projection

7.4.2. Using aggregation

7.4.3. Grouping

7.4.4. Restricting groups with having

7.4.5. Improving performance with report queries

7.5. Advanced query techniques

7.5.1. Dynamic queries

7.5.2. Collection filters

7.5.3. Subqueries

7.5.4. Native SQL queries

7.6. Optimizing object retrieval

7.6.1. Solving the n+1 selects problem

7.6.2. Using iterate() queries

7.6.3. Caching queries

7.7. Summary

8. Writing Hibernate applications

8.1. Designing layered applications

8.1.1. Using Hibernate in a servlet engine

8.1.2. Using Hibernate in an EJB container

8.2. Implementing application transactions

8.2.1. Approving a new auction

8.2.2. Doing it the hard way

8.2.3. Using detached persistent objects

8.2.4. Using a long session

8.2.5. Choosing an approach to application transactions

8.3. Handling special kinds of data

8.3.1. Legacy schemas and composite keys

8.3.2. Audit logging

8.4. Summary

9. Using the toolset

9.1. Development processes

9.1.1. Top down

9.1.2. Bottom up

9.1.3. Middle out (metadata oriented)

9.1.4. Meet in the middle

9.1.5. Roundtripping

9.2. Automatic schema generation

9.2.1. Preparing the mapping metadata

9.2.2. Creating the schema

9.2.3. Updating the schema

9.3. Generating POJO code

9.3.1. Adding meta-attributes

9.3.2. Generating finders

9.3.3. Configuring hbm2java

9.3.4. Running hbm2java

9.4. Existing schemas and Middlegen

9.4.1. Starting Middlegen

9.4.2. Restricting tables and relationships

9.4.3. Customizing the metadata generation

9.4.4. Generating hbm2java and XDoclet metadata

9.5. XDoclet

9.5.1. Setting value type attributes

9.5.2. Mapping entity associations

9.5.3. Running XDoclet

9.6. Summary

Appendix A: SQL fundamentals

Appendix B: ORM implementation strategies

B.1. Properties or fields?

B.2. Dirty-checking strategies

Appendix C: Back in the real world

C.1. The strange copy

C.2. The more the better

C.3. We don’t need primary keys

C.4. Time isn’t linear

C.5. Dynamically unsafe

C.6. To synchronize or not?

C.7. Really fat client

C.8. Resuming Hibernate



About the Technology

Hibernate practically exploded on the Java scene. Why is this open-source tool so popular? Because it automates a tedious task: persisting your Java objects to a relational database. The inevitable mismatch between your object-oriented code and the relational database requires you to write code that maps one to the other. This code is often complex, tedious and costly to develop. Hibernate does the mapping for you.

Not only that, Hibernate makes it easy. Positioned as a layer between your application and your database, Hibernate takes care of loading and saving of objects. Hibernate applications are cheaper, more portable, and more resilient to change. And they perform better than anything you are likely to develop yourself.

What's inside

  • ORM concepts
  • Getting started
  • Many real-world tasks
  • The Hibernate application development processes

About the authors

A member of the core Hibernate developer team, Christian Bauer maintains the Hibernate documentation and website. He is a senior software engineer in Frankfurt, Germany. Gavin King is the Hibernate founder and principal developer. He is a J2EE consultant based in Melbourne, Australia.

The Bible of Hibernate

Ara Abrahamian, XDoclet Lead Developer