Java Persistence with Hibernate
Second Edition of Hibernate in Action
Christian Bauer and Gavin King
  • November 2006
  • ISBN 9781932394887
  • 880 pages
  • printed in black & white

...this book is the ultimate solution. If you are going to use Hibernate in your application, you have no other choice, go rush to the store and get this book.

Meera Subbarao, JavaLobby

Java Persistence with Hibernate explores Hibernate by developing an application that ties together hundreds of individual examples. You'll immediately dig into the rich programming model of Hibernate 3.2 and Java Persistence, working through queries, fetching strategies, caching, transactions, conversations, and more. You'll also appreciate the well-illustrated discussion of best practices in database design, object/relational mapping, and optimization techniques.

In this revised edition of Manning's bestselling Hibernate in Action, authors Christian Bauer and Gavin King—the founder of the Hibernate project—cover Hibernate 3.2 in detail along with the EJB 3.0 and Java Persistence 1.0 standards.

The book ends with detailed coverage of JBoss Seam, a revolutionary web application framework for Java EE that builds on EJB 3.0, JavaServer Faces, and Hibernate concepts.

Table of Contents show full

foreword to the revised edition

foreword to the first edition

preface to the revised edition

preface to the first edition

acknowledgments

about this book

about the cover illustration

Part 1 Getting started with Hibernate and EJB 3.0

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 data 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. Object-oriented database systems

1.3.5. 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.4.4. Introducing Hibernate, EJB3, and JPA

1.5. Summary

2. Starting a project

2.1. Starting a Hibernate project

2.1.1. Selecting a development process

2.1.2. Setting up the project

2.1.3. Hibernate configuration and startup

2.1.4. Running and testing the application

2.2. Starting a Java Persistence project

2.2.1. Using Hibernate Annotations

2.2.2. Using Hibernate EntityManager

2.2.3. Introducing EJB components

2.2.4. Switching to Hibernate interfaces

2.3. Reverse engineering a legacy database

2.3.1. Creating a database configuration

2.3.2. Customizing reverse engineering

2.3.3. Generating Java source code

2.4. Integration with Java EE services

2.4.1. Integration with JTA

2.4.2. JNDI-bound SessionFactory

2.4.3. JMX service deployment

2.5. Summary

3. Domain models and metadata

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 and persistent entity classes

3.2.4. Implementing POJO associations

3.2.5. Adding logic to accessor methods

3.3. Object/relational mapping metadata

3.3.1. Metadata in XML

3.3.2. Annotation-based metadata

3.3.3. Using XDoclet

3.3.4. Handling global metadata

3.3.5. Manipulating metadata at runtime

3.4. Alternative entity representation

3.4.1. Creating dynamic applications

3.4.2. Representing data in XML

3.5. Summary

Part 2 Mapping concepts and strategies

4. Mapping persistent classes

4.1. Understanding entities and value types

4.1.1. Fine-grained domain models

4.1.2. Defining the concept

4.1.3. Identifying entities and value types

4.2. Mapping entities with identity

4.2.1. Understanding Java identity and equality

4.2.2. Handling database identity

4.2.3. Database primary keys

4.3. Class mapping options

4.3.1. Dynamic SQL generation

4.3.2. Making an entity immutable

4.3.3. Naming entities for querying

4.3.4. Declaring a package name

4.3.5. Quoting SQL identifiers

4.3.6. Implementing naming conventions

4.4. Fine-grained models and mappings

4.4.1. Mapping basic properties

4.4.2. Mapping components

4.5. Summary

5. Inheritance and custom types

5.1. Mapping class inheritance

5.1.1. Table per concrete class with implicit polymorphism

5.1.2. Table per concrete class with unions

5.1.3. Table per class hierarchy

5.1.4. Table per subclass

5.1.5. Mixing inheritance strategies

5.1.6. Choosing a strategy

5.2. The Hibernate type system

5.2.1. Recapitulating entity and value types

5.2.2. Built-in mapping types

5.2.3. Using mapping types

5.3. Creating custom mapping types

5.3.1. Considering custom mapping types

5.3.2. The extension points

5.3.3. The case for custom mapping types

5.3.4. Creating a UserType

5.3.5. Creating a CompositeUserType

5.3.6. Parameterizing custom types

5.3.7. Mapping enumerations

5.4. Summary

6. Mapping collections and entity associations

6.1. Sets, bags, lists, and maps of value types

6.1.1. Selecting a collection interface

6.1.2. Mapping a set

6.1.3. Mapping an identifier bag

6.1.4. Mapping a list

6.1.5. Mapping a map

6.1.6. Sorted and ordered collections

6.2. Collections of components

6.2.1. Writing the component class

6.2.2. Mapping the collection

6.2.3. Enabling bidirectional navigation

6.2.4. Avoiding not-null columns

6.3. Mapping collections with annotations

6.3.1. Basic collection mapping

6.3.2. Sorted and ordered collections

6.3.3. Mapping a collection of embedded objects

6.4. Mapping a parent/children relationship

6.4.1. Multiplicity

6.4.2. The simplest possible association

6.4.3. Making the association bidirectional

6.4.4. Cascading object state

6.5. Summary

7. Advanced entity association mappings

7.1. Single-valued entity associations

7.1.1. Shared primary key associations

7.1.2. One-to-one foreign key associations

7.1.3. Mapping with a join table

7.2. Many-valued entity associations

7.2.1. One-to-many associations

7.2.2. Many-to-many associations

7.2.3. Adding columns to join tables

7.2.4. Mapping maps

7.3. Polymorphic associations

7.3.1. Polymorphic many-to-one associations

7.3.2. Polymorphic collections

7.3.3. Polymorphic associations to unions

7.3.4. Polymorphic table per concrete class

7.4. Summary

8. Legacy databases and custom SQL

8.1. Integrating legacy databases

8.1.1. Handling primary keys

8.1.2. Arbitrary join conditions with formulas

8.1.3. Joining arbitrary tables

8.1.4. Working with triggers

8.2. Customizing SQL

8.2.1. Writing custom CRUD statements

8.2.2. Integrating stored procedures and functions

8.3. Improving schema DDL

8.3.1. Custom SQL names and datatypes

8.3.2. Ensuring data consistency

8.3.3. Adding domains and column constraints

8.3.4. Table-level constraints

8.3.5. Database constraints

8.3.6. Creating indexes

8.3.7. Adding auxiliary DDL

8.4. Summary

Part 3 Conversational object processing

9. Working with objects

9.1. The persistence lifecycle

9.1.1. Object states

9.1.2. The persistence context

9.2. Object identity and equality

9.2.1. Introducing conversations

9.2.2. The scope of object identity

9.2.3. The identity of detached objects

9.2.4. Extending a persistence context

9.3. The Hibernate interfaces

9.3.1. Storing and loading objects

9.3.2. Working with detached objects

9.3.3. Managing the persistence context

9.4. The Java Persistence API

9.4.1. Storing and loading objects

9.4.2. Working with detached entity instances

9.5. Using Java Persistence in EJB components

9.5.1. Injecting an EntityManager

9.5.2. Looking up an EntityManager

9.5.3. Accessing an EntityManagerFactory

9.6. Summary

10. Transactions and concurrency

10.1. Transaction essentials

10.1.1. Database and system transactions

10.1.2. Transactions in a Hibernate application

10.1.3. Transactions with Java Persistence

10.2. Controlling concurrent access

10.2.1. Understanding database-level concurrency

10.2.2. Optimistic concurrency control

10.2.3. Obtaining additional isolation guarantees

10.3. Nontransactional data access

10.3.1. Debunking autocommit myths

10.3.2. Working nontransactionally with Hibernate

10.3.3. Optional transactions with JTA

10.4. Summary

11. Implementing conversations

11.1. Propagating the Hibernate Session

11.1.1. The use case for Session propagation

11.1.2. Propagation through thread-local

11.1.3. Propagation with JTA

11.1.4. Propagation with EJBs

11.2. Conversations with Hibernate

11.2.1. Providing conversational guarantees

11.2.2. Conversations with detached objects

11.2.3. Extending a Session for a conversation

11.3. Conversations with JPA

11.3.1. Persistence context propagation in Java SE

11.3.2. Merging detached objects in conversations

11.3.3. Extending the persistence context in Java SE

11.4. Conversations with EJB 3.0

11.4.1. Context propagation with EJBs

11.4.2. Extended persistence contexts with EJBs

11.5. Summary

12. Modifying objects efficiently

12.1. Transitive persistence

12.1.1. Persistence by reachability

12.1.2. Applying cascading to associations

12.1.3. Working with transitive state

12.1.4. Transitive associations with JPA

12.2. Bulk and batch operations

12.2.1. Bulk statements with HQL and JPA QL

12.2.2. Processing with batches

12.2.3. Using a stateless Session

12.3. Data filtering and interception

12.3.1. Dynamic data filters

12.3.2. Intercepting Hibernate events

12.3.3. The core event system

12.3.4. Entity listeners and callbacks

12.4. Summary

13. Optimizing fetching and caching

13.1. Defining the global fetch plan

13.1.1. The object-retrieval options

13.1.2. The lazy default fetch plan

13.1.3. Understanding proxies

13.1.4. Disabling proxy generation

13.1.5. Eager loading of associations and collections

13.1.6. Lazy loading with interception

13.2. Selecting a fetch strategy

13.2.1. Prefetching data in batches

13.2.2. Prefetching collections with subselects

13.2.3. Eager fetching with joins

13.2.4. Optimizing fetching for secondary tables

13.2.5. Optimization guidelines

13.3. Caching fundamentals

13.3.1. Caching strategies and scopes

13.3.2. The Hibernate cache architecture

13.4. Caching in practice

13.4.1. Selecting a concurrency control strategy

13.4.2. Understanding cache regions

13.4.3. Setting up a local cache provider

13.4.4. Setting up a replicated cache

13.4.5. Controlling the second-level cache

13.5. Summary

14. Querying with HQL and JPA QL

14.1. Creating and running queries

14.1.1. Preparing a query

14.1.2. Executing a query

14.1.3. Using named queries

14.2. Basic HQL and JPA QL queries

14.2.1. Selection

14.2.2. Restriction

14.2.3. Projection

14.3. Joins, reporting queries, and subselects

14.3.1. Joining relations and associations

14.3.2. Reporting queries

14.3.3. Using subselects

14.4. Summary

15. Advanced query options

15.1. Querying with criteria and example

15.1.1. Basic criteria queries

15.1.2. Joins and dynamic fetching

15.1.3. Projection and report queries

15.1.4. Query by example

15.2. Using native SQL queries

15.2.1. Automatic resultset handling

15.2.2. Retrieving scalar values

15.2.3. Native SQL in Java Persistence

15.3. Filtering collections

15.4. Caching query results

15.4.1. Enabling the query result cache

15.4.2. Understanding the query cache

15.4.3. When to use the query cache

15.4.4. Natural identifier cache lookups

15.5. Summary

16. Creating and testing layered applications

16.1. Hibernate in a web application

16.1.1. Introducing the use case

16.1.2. Writing a controller

16.1.3. The Open Session in View pattern

16.1.4. Designing smart domain models

16.2. Creating a persistence layer

16.2.1. A generic data-access object pattern

16.2.2. Implementing the generic CRUD interface

16.2.3. Implementing entity DAOs

16.2.4. Using data-access objects

16.3. Introducing the Command pattern

16.3.1. The basic interfaces

16.3.2. Executing command objects

16.3.3. Variations of the Command pattern

16.4. Designing applications with EJB 3.0

16.4.1. Implementing a conversation with stateful beans

16.4.2. Writing DAOs with EJBs

16.4.3. Utilizing dependency injection

16.5. Testing

16.5.1. Understanding different kinds of tests

16.5.2. Introducing TestNG

16.5.3. Testing the persistence layer

16.5.4. Considering performance benchmarks

16.6. Summary

17. Introducing JBoss Seam

17.1. The Java EE 5.0 programming model

17.1.1. Considering JavaServer Faces

17.1.2. Considering EJB 3.0

17.1.3. Writing a web application with JSF and EJB 3.0

17.1.4. Analyzing the application

17.2. Improving the application with Seam

17.2.1. Configuring Seam

17.2.2. Binding pages to stateful Seam components

17.2.3. Analyzing the Seam application

17.3. Understanding contextual components

17.3.1. Writing the login page

17.3.2. Creating the components

17.3.3. Aliasing contextual variables

17.3.4. Completing the login/logout feature

17.4. Validating user input

17.4.1. Introducing Hibernate Validator

17.4.2. Creating the registration page

17.4.3. Internationalization with Seam

17.5. Simplifying persistence with Seam

17.5.1. Implementing a conversation

17.5.2. Letting Seam manage the persistence context

17.6. Summary

Appendix A: SQL fundamentals

Appendix B: Mapping quick reference

references

index

About the Technology

Persistence—the ability of data to outlive an instance of a program—is central to modern applications. Hibernate, the most popular Java persistence tool, provides automatic and transparent object/relational mapping so it's a snap to work with SQL databases in Java applications. Hibernate conforms to the new EJB 3.0 and Java Persistence 1.0 standards.

What's inside

  • O/R mapping concepts
  • Get started with Hibernate and Java Persistence
  • Efficient database application design
  • Comprehensive Hibernate and Java Persistence reference
  • Unmatched breadth and depth

About the authors

Christian Bauer is a member of the Hibernate developer team and a trainer, consultant, and product manager for Hibernate, EJB 3.0, and JBoss Seam at JBoss.

Gavin King, a lead developer at JBoss, is the founder of the Hibernate project, and a member of the EJB 3.0 (JSR 220) expert group. He also leads the Web Beans JSR 299, a standardization effort involving Hibernate concepts, JSF, and EJB 3.0.


eBook $47.99 PDF only

FREE domestic shipping on three or more pBooks

"Java Persistence with Hibernate" is the most comprehensive, authoritative and definitive guide, tutorial, and reference to OR-mapped Java persistence.

Will Wagers, C#online.net

A definitive source on Hibernate. Great for any developer.

Patrick Peak, CTO, BrowserMedia, Author of Hibernate Quickly

I wholeheartedly recommend this book!

Stuart Caborn, Thoughtworks

Great topic, great content, and there is a great need for this book!

Ryan Daigle, RTP Region, ALTERthought

...very thorough, very complete. I highly recommend it.

Patrick Dennis, Java Developer Management Dynamics Inc.

A must-read for developers using Hibernate.

Dan Dobrin, BEA Systems

It's the best book out there about Hibernate (and there are quite a few on the market right now), maybe even the best book about ORM in Java, in general.

Adrian Spinei, Real Life, beta version