Java Persistence with Hibernate, Second Edition
Christian Bauer, Gavin King, and Gary Gregory
  • MEAP began March 2013
  • Publication in November 2015 (estimated)
  • ISBN 9781617290459
  • 600 pages (estimated)
  • printed in black & white

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, working through mappings, queries, fetching strategies, transactions, conversations, caching, and more. You'll also appreciate the well-illustrated discussion of best practices in database design and optimization techniques. In this revised edition, authors Christian Bauer, Gavin King, and Gary Gregory cover Hibernate 5 in detail along with the Java Persistence 2.1 standard (JSR 338).

All examples have been updated for the latest Hibernate and Java EE specification versions.

Table of Contents show full

Part 1: Getting started with ORM

1. Introducing JPA and Hibernate

1.1. What is persistence?

1.1.1. Relational databases

1.1.2. Understanding SQL

1.1.3. Using SQL in Java

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.3. Hello World with Hibernate

1.3.1. ORM and JPA

1.3.2. Introducing Hibernate

1.3.3. Starting a project

1.3.4. Mapping a class

1.3.5. Storing and loading data

1.4. Summary

2. Domain models and metadata

2.1. The CaveatEmptor application

2.1.1. A layered architecture

2.1.2. Analyzing the business domain

2.1.3. The CaveatEmptor domain model

2.2. Implementing the domain model

2.2.1. Addressing leakage of concerns

2.2.2. Transparent and automated persistence

2.2.3. Writing persistence-capable classes

2.2.4. Implementing POJO associations

2.3. Domain model metadata

2.3.1. Annotation-based metadata

2.3.2. Applying Bean Validation rules

2.3.3. Externalizing metadata with XML files

2.3.4. Accessing metadata at runtime

2.4. Summary

Part 2: Mapping strategies

3. Mapping persistent classes

3.1. Understanding entities and value types

3.1.1. Fine-grained domain models

3.1.2. Distinguishing entities and value types

3.2. Mapping entities with identity

3.2.1. Understanding Java identity and equality

3.2.2. A first entity class and mapping

3.2.3. Selecting a primary key

3.2.4. Configuring key generators

3.2.5. Identifier generator strategies

3.3. Entity mapping options

3.3.1. Controlling names

3.3.2. Dynamic SQL generation

3.3.3. Making an entity immutable

3.3.4. Mapping an entity to a subselect

3.4. Summary

4. Mapping value types

4.1. Basic properties

4.1.1. Overriding basic property defaults

4.1.2. Using derived properties

4.1.3. Transforming column values

4.1.4. Generated and default property values

4.1.5. Temporal properties

4.1.6. Mapping enumerations

4.2. Mapping embeddable components

4.2.1. The database schema

4.2.2. Making classes embeddable

4.2.3. Overriding embedded attributes

4.2.4. Mapping nested embedded components

4.3. Types and converters

4.3.1. Built-in types

4.3.2. Creating custom JPA converters

4.3.3. Extending Hibernate with UserTypes

4.4. Summary

5. Mapping inheritance

5.1. Table per concrete class with implicit polymorphism

5.2. Table per concrete class with unions

5.3. Table per class hierarchy

5.4. Table per subclass with joins

5.5. Mixing inheritance strategies

5.6. Inheritance of embeddable classes

5.7. Choosing a strategy

5.8. Polymorphic associations

5.8.1. Polymorphic many-to-one associations

5.8.2. Polymorphic collections

5.9. 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. Equality of component instances

6.2.2. Set of components

6.2.3. Bag of components

6.2.4. Map of component values

6.2.5. Components as map keys

6.2.6. Collection in an embeddable component

6.3. Mapping entity associations

6.3.1. The simplest possible association

6.3.2. Making it bidirectional

6.3.3. Cascading state

6.4. Summary

7. Advanced entity association mappings

7.1. One-to-one associations

7.1.1. Sharing a primary key

7.1.2. The foreign primary key generator

7.1.3. Using a foreign key join column

7.1.4. Using a join table

7.2. One-to-many associations

7.2.1. Considering one-to-many bags

7.2.2. Uni-directional and bidirectional list mappings

7.2.3. Optional one-to-many with a join table

7.2.4. One-to-many association in an embeddable class

7.3. Many-to-many and ternary associations

7.3.1. Uni- and bidirectional many-to-many

7.3.2. Many-to-many with an intermediate entity

7.3.3. Ternary associations with components

7.4. Entity associations with Maps

7.4.1. One-to-many with property key

7.4.2. Key/Value ternary relationship

7.5. Summary

8. Complex and legacy schemas

8.1. Improving the database schema

8.1.1. Adding auxiliary database objects

8.1.2. SQL constraints

8.1.3. Creating indexes

8.2. Handling legacy keys

8.2.1. Mapping a natural primary key

8.2.2. Mapping a composite primary key

8.2.3. Foreign keys in composite primary keys

8.2.4. Foreign keys to composite primary keys

8.2.5. Foreign key referencing non-primary keys

8.3. Mapping properties to secondary tables

8.4. Summary

Part 3: Transactional data processing

9. Managing data

9.1. The persistence life cycle

9.1.1. Entity instance states

9.1.2. The persistence context

9.2. The EntityManager interface

9.2.1. The canonical unit of work

9.2.2. Making data persistent

9.2.3. Retrieving and modifying persistent data

9.2.4. Getting a reference

9.2.5. Making data transient

9.2.6. Refreshing data

9.2.7. Replicating data

9.2.8. Caching in the persistence context

9.2.9. Flushing the persistence context

9.3. Working with detached state

9.3.1. The identity of detached instances

9.3.2. Implementing equality methods

9.3.3. Detaching entity instances

9.3.4. Merging entity instances

9.4. Summary

10. Transactions and concurrency

10.1. Transaction essentials

10.1.1. ACID attributes

10.1.2. Database and system transactions

10.1.3. Programmatic transactions with JTA

10.2. Controlling concurrent access

10.2.1. Understanding database-level concurrency

10.2.2. Optimistic concurrency control

10.2.3. Explicit pessimistic locking

10.2.4. Avoiding deadlocks

10.3. Non-transactional data access

10.3.1. Reading data in auto-commit mode

10.3.2. Queueing modifications

10.4. Summary

11. Fetch plans, strategies, and profiles

11.1. Lazy and eager loading

11.1.1. Lazy persistent collections

11.1.2. Lazy loading with interception

11.1.3. Eager loading of associations and collections

11.2. Selecting a fetch strategy

11.2.1. The n+1 selects problem

11.2.2. The Cartesian product problem

11.2.3. Prefetching data in batches

11.2.4. Prefetching collections with subselects

11.2.5. Eager fetching with multiple SELECTs

11.2.6. Dynamic eager fetching

11.3. Using fetch profiles

11.3.1. Declaring Hibernate fetch profiles

11.3.2. Working with entity graphs

11.4. Summary

12. Filtering data

12.1. Cascading state transitions

12.1.1. Transitive detachment and merging

12.1.2. Cascading refresh

12.1.3. Cascading replication

12.1.4. Enabling global transitive persistence

12.2. Listening to and intercepting events

12.2.1. JPA event listeners and callbacks

12.2.2. Implementing Hibernate interceptors

12.2.3. The core event system

12.3. Auditing and versioning with Hibernate Envers

12.3.1. Enabling audit logging

12.3.2. Creating an audit trail

12.3.3. Finding revisions

12.3.4. Accessing historical data

12.4. Dynamic data filters

12.4.1. Defining dynamic filters

12.4.2. Applying the filter

12.4.3. Enabling the filter

12.4.4. Filtering collection access

12.4.5. Supporting multiple database tenants

12.5. Summary

Part 4: Writing queries

13. Creating and executing queries

13.1. Creating queries

13.1.1. The JPA query interfaces

13.1.2. Typed query results

13.1.3. Hibernate’s query interfaces

13.2. Preparing queries

13.2.1. Protecting against SQL injection attacks

13.2.2. Binding named parameters

13.2.3. Using positional parameters

13.2.4. Paging through large result sets

13.3. Executing queries

13.3.1. Listing all results

13.3.2. Getting a single result

13.3.3. Scrolling with database cursors

13.3.4. Iterating through a result

13.4. Naming and externalizing queries

13.4.1. Calling a named query

13.4.2. Defining queries in XML metadata

13.4.3. Defining queries with annotations

13.4.4. Defining named queries programmatically

13.5. Query hints

13.5.1. Setting a timeout

13.5.2. Setting the flush mode

13.5.3. Setting read-only mode

13.5.4. Setting a fetch size

13.5.5. Setting an SQL comment

13.5.6. Named query hints

13.6. Summary

14. The query languages

14.1. Selection

14.1.1. Assigning aliases and query roots

14.1.2. Polymorphic queries

14.2. Restriction

14.2.1. Comparison expressions

14.2.2. Expressions with collections

14.2.3. Calling functions

14.2.4. Ordering query results

14.3. Projection

14.3.1. Projection of entities and scalar values

14.3.2. Utilizing dynamic instantiation

14.3.3. Getting distinct results

14.3.4. Calling functions in projections

14.3.5. Aggregation functions

14.3.6. Grouping

14.4. Joins

14.4.1. Joins with SQL

14.4.2. Join options in JPA

14.4.3. Implicit association joins

14.4.4. Explicit joins

14.4.5. Dynamic fetching with joins

14.4.6. Theta-style joins

14.4.7. Comparing identifiers

14.5. Subselects

14.5.1. Correlated and uncorrelated nesting

14.5.2. Quantification

14.6. Summary

15. Advanced query options

15.1. Transforming query results

15.1.1. Returning list of lists

15.1.2. Returning a list of maps

15.1.3. Mapping aliases to bean properties

15.1.4. Writing a ResultTransformer

15.2. Filtering collections

15.3. The Hibernate criteria query API

15.3.1. Selection and ordering

15.3.2. Restriction

15.3.3. Projection and aggregation

15.3.4. Joins

15.3.5. Subselects

15.3.6. Example queries

15.4. Summary

16. Customizing SQL

16.1. Falling back to JDBC

16.2. Mapping SQL query results

16.3. Customizing CRUD operations

16.3.1. Enabling custom loaders

16.3.2. Customizing creation, updates, and deletion

16.3.3. Customizing collection operations

16.3.4. Eager fetching in custom loaders

16.4. Calling stored procedures

16.4.1. Returning a result set

16.4.2. Returning multiple results and update counts

16.4.3. Setting input and output parameters

16.4.4. Returning a cursor

16.5. Using stored procedures for CRUD

16.5.1. Custom loader with a procedure

16.5.2. Procedures for CUD

16.6. Summary

Part 5: Building Applications

17. Designing client/server applications

17.1. Creating a persistence layer

17.1.1. Implementing the generic interface

17.1.2. Implementing entity DAOs

17.1.3. Testing the persistence layer

17.2. Building a stateless server

17.2.1. Editing an auction item

17.2.2. Placing a bid

17.2.3. Analyzing the stateless application

17.3. Building a stateful server

17.3.1. Editing an auction item

17.3.2. Analyzing the stateful application

17.4. Summary

18. Building web applications

18.1. Integrating JPA with CDI

18.1.1. Producing an EntityManager

18.1.2. Joining the EntityManager with transactions

18.1.3. Injecting an EntityManager

18.2. Paging and sorting data

18.2.1. Offset Paging vs. Seeking

18.2.2. Paging in the persistence layer

18.2.3. Querying page-by-page

18.3. Building JSF applications

18.3.1. Request-scoped services

18.3.2. Conversation-scoped services

18.4. Serializing domain model data

18.4.1. Writing a JAX-RS service

18.4.2. Applying JAXB mappings

18.4.3. Serializing Hibernate proxies

18.5. Summary

19. Scaling Hibernate

19.1. Bulk and batch processing

19.1.1. Bulk statements in JPQL and criteria

19.1.2. Bulk statements in SQL

19.1.3. Processing in batches

19.1.4. The Hibernate StatelessSession

19.2. Caching data

19.2.1. The Hibernate shared cache architecture

19.2.2. Configuring the shared cache

19.2.3. Enabling entity and collection caching

19.2.4. Testing the shared cache

19.2.5. Setting cache modes

19.2.6. Controlling the shared cache

19.2.7. The query result cache

19.3. Summary


Appendix A: Introducing Hibernate OGM

About the Technology

Persistence—the ability of data to outlive an instance of a program—is central to modern applications. Hibernate, the most popular provider of the Java Persistence standard, offers automatic and transparent object/relational mapping, making it a snap to work with SQL databases in Java applications.

What's inside

  • Object/relational mapping concepts
  • Get started with Hibernate and Java Persistence
  • Efficient database application design
  • Comprehensive Hibernate and Java Persistence reference
  • Integration of Java Persistence with EJB, CDI, JSF, and JAX-RS
  • Unmatched breadth and depth

About the authors

Christian Bauer is a member of the Hibernate developer team and he works as a trainer and consultant.

Gavin King is the founder of the Hibernate project and a member of the original Java Persistence expert group (JSR 220). He also led the standardization effort of CDI (JSR 299). Gavin is currently creating a new programming language for business applications called Ceylon.

Gary Gregory is a software engineer at Rocket Software working on application servers and legacy integration. He is the co-author of JUnit in Action and Spring Batch in Action, and a member of the project management committees for the Apache Software Foundation project Commons, Xalan and Logging Services.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $59.99 pBook + eBook
MEAP eBook $47.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks