Java Persistence with Hibernate, Second Edition

Book description

Java Persistence with Hibernate, Second Edition explores Hibernate by developing an application that ties together hundreds of individual examples. In this revised edition, authors Christian Bauer, Gavin King, and Gary Gregory cover Hibernate 5 in detail with the Java Persistence 2.1 standard (JSR 338). All examples have been updated for the latest Hibernate and Java EE specification versions.

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, offers automatic and transparent object/relational mapping, making it a snap to work with SQL databases in Java applications.



About the Book
Java Persistence with Hibernate, Second Edition 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. Along the way you'll find a 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 with the Java Persistence 2.1 standard (JSR 338). All examples have been updated for the latest Hibernate and Java EE specification versions.

What's Inside
  • Object/relational mapping concepts
  • 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 Reader
The book assumes a working knowledge of Java.

About the Authors
Christian Bauer is a member of the Hibernate developer team and a trainer and consultant. Gavin King is the founder of the Hibernate project and a member of the Java Persistence expert group (JSR 220). Gary Gregory is a principal software engineer working on application servers and legacy integration.

Quotes
The most comprehensive book about Hibernate Persistence ... works well both as a tutorial and as a reference.
- Sergio Fernandez Gonzalez, Accenture Software

The essential guidebook for navigating the intricacies of Hibernate.
- José Diaz, OptumHealth

An excellent update to a classic and essential book.
- Jerry Goodnough, Cognitive Medical Systems

The must-have reference for every Hibernate user.
- Stephan Heffner, SPIEGEL-Verlag Rudolf Augstein GmbH & Co. KG

Table of contents

  1. Java Persistence with Hibernate
  2. Praise for the First Edition
  3. Copyright
  4. Dedication
  5. Brief Table of Contents
  6. Table of Contents
  7. front matter
    1. Foreword to the First Edition
    2. Preface
    3. Acknowledgments
    4. About this Book
      1. Roadmap
      2. Who should read this book?
      3. Code conventions
      4. Source code downloads
      5. Author Online
      6. About the authors
    5. About the Cover Illustration
  8. Part 1. Getting started with ORM
  9. Chapter 1. Understanding object/relational persistence
    1. 1.1. What is persistence?
      1. 1.1.1. Relational databases
      2. 1.1.2. Understanding SQL
      3. 1.1.3. Using SQL in Java
    2. 1.2. The paradigm mismatch
      1. 1.2.1. The problem of granularity
      2. 1.2.2. The problem of subtypes
      3. 1.2.3. The problem of identity
      4. 1.2.4. Problems relating to associations
      5. 1.2.5. The problem of data navigation
    3. 1.3. ORM and JPA
    4. 1.4. Summary
  10. Chapter 2. Starting a project
    1. 2.1. Introducing Hibernate
    2. 2.2. “Hello World” with JPA
      1. 2.2.1. Configuring a persistence unit
      2. 2.2.2. Writing a persistent class
      3. 2.2.3. Storing and loading messages
    3. 2.3. Native Hibernate configuration
    4. 2.4. Summary
  11. Chapter 3. Domain models and metadata
    1. 3.1. The example CaveatEmptor application
      1. 3.1.1. A layered architecture
      2. 3.1.2. Analyzing the business domain
      3. 3.1.3. The CaveatEmptor domain model
    2. 3.2. Implementing the domain model
      1. 3.2.1. Addressing leakage of concerns
      2. 3.2.2. Transparent and automated persistence
      3. 3.2.3. Writing persistence-capable classes
      4. 3.2.4. Implementing POJO associations
    3. 3.3. Domain model metadata
      1. 3.3.1. Annotation-based metadata
      2. 3.3.2. Applying Bean Validation rules
      3. 3.3.3. Externalizing metadata with XML files
      4. 3.3.4. Accessing metadata at runtime
    4. 3.4. Summary
  12. Part 2. Mapping strategies
  13. Chapter 4. Mapping persistent classes
    1. 4.1. Understanding entities and value types
      1. 4.1.1. Fine-grained domain models
      2. 4.1.2. Defining application concepts
      3. 4.1.3. Distinguishing entities and value types
    2. 4.2. Mapping entities with identity
      1. 4.2.1. Understanding Java identity and equality
      2. 4.2.2. A first entity class and mapping
      3. 4.2.3. Selecting a primary key
      4. 4.2.4. Configuring key generators
      5. 4.2.5. Identifier generator strategies
    3. 4.3. Entity-mapping options
      1. 4.3.1. Controlling names
      2. 4.3.2. Dynamic SQL generation
      3. 4.3.3. Making an entity immutable
      4. 4.3.4. Mapping an entity to a subselect
    4. 4.4. Summary
  14. Chapter 5. Mapping value types
    1. 5.1. Mapping basic properties
      1. 5.1.1. Overriding basic property defaults
      2. 5.1.2. Customizing property access
      3. 5.1.3. Using derived properties
      4. 5.1.4. Transforming column values
      5. 5.1.5. Generated and default property values
      6. 5.1.6. Temporal properties
      7. 5.1.7. Mapping enumerations
    2. 5.2. Mapping embeddable components
      1. 5.2.1. The database schema
      2. 5.2.2. Making classes embeddable
      3. 5.2.3. Overriding embedded attributes
      4. 5.2.4. Mapping nested embedded components
    3. 5.3. Mapping Java and SQL types with converters
      1. 5.3.1. Built-in types
      2. 5.3.2. Creating custom JPA converters
      3. 5.3.3. Extending Hibernate with UserTypes
    4. The extension points
    5. 5.4. Summary
  15. Chapter 6. Mapping inheritance
    1. 6.1. Table per concrete class with implicit polymorphism
    2. 6.2. Table per concrete class with unions
    3. 6.3. Table per class hierarchy
    4. 6.4. Table per subclass with joins
    5. 6.5. Mixing inheritance strategies
    6. 6.6. Inheritance of embeddable classes
    7. 6.7. Choosing a strategy
    8. 6.8. Polymorphic associations
      1. 6.8.1. Polymorphic many-to-one associations
      2. 6.8.2. Polymorphic collections
    9. 6.9. Summary
  16. Chapter 7. Mapping collections and entity associations
    1. 7.1. Sets, bags, lists, and maps of value types
      1. 7.1.1. The database schema
      2. 7.1.2. Creating and mapping a collection property
      3. 7.1.3. Selecting a collection interface
      4. 7.1.4. Mapping a set
      5. 7.1.5. Mapping an identifier bag
      6. 7.1.6. Mapping a list
      7. 7.1.7. Mapping a map
      8. 7.1.8. Sorted and ordered collections
    2. 7.2. Collections of components
      1. 7.2.1. Equality of component instances
      2. 7.2.2. Set of components
      3. 7.2.3. Bag of components
      4. 7.2.4. Map of component values
      5. 7.2.5. Components as map keys
      6. 7.2.6. Collection in an embeddable component
    3. 7.3. Mapping entity associations
      1. 7.3.1. The simplest possible association
      2. 7.3.2. Making it bidirectional
      3. 7.3.3. Cascading state
    4. 7.4. Summary
  17. Chapter 8. Advanced entity association mappings
    1. 8.1. One-to-one associations
      1. 8.1.1. Sharing a primary key
      2. 8.1.2. The foreign primary key generator
      3. 8.1.3. Using a foreign key join column
      4. 8.1.4. Using a join table
    2. 8.2. One-to-many associations
      1. 8.2.1. Considering one-to-many bags
      2. 8.2.2. Unidirectional and bidirectional list mappings
      3. 8.2.3. Optional one-to-many with a join table
      4. 8.2.4. One-to-many association in an embeddable class
    3. 8.3. Many-to-many and ternary associations
      1. 8.3.1. Unidirectional and bidirectional many-to-many associations
      2. 8.3.2. Many-to-many with an intermediate entity
      3. 8.3.3. Ternary associations with components
    4. 8.4. Entity associations with Maps
      1. 8.4.1. One-to-many with a property key
      2. 8.4.2. Key/Value ternary relationship
    5. 8.5. Summary
  18. Chapter 9. Complex and legacy schemas
    1. 9.1. Improving the database schema
      1. 9.1.1. Adding auxiliary database objects
      2. 9.1.2. SQL constraints
      3. 9.1.3. Creating indexes
    2. 9.2. Handling legacy keys
      1. 9.2.1. Mapping a natural primary key
      2. 9.2.2. Mapping a composite primary key
      3. 9.2.3. Foreign keys in composite primary keys
      4. 9.2.4. Foreign keys to composite primary keys
      5. 9.2.5. Foreign key referencing non-primary keys
    3. 9.3. Mapping properties to secondary tables
    4. 9.4. Summary
  19. Part 3. Transactional data processing
  20. Chapter 10. Managing data
    1. 10.1. The persistence life cycle
      1. 10.1.1. Entity instance states
      2. 10.1.2. The persistence context
    2. 10.2. The EntityManager interface
      1. 10.2.1. The canonical unit of work
      2. 10.2.2. Making data persistent
      3. 10.2.3. Retrieving and modifying persistent data
      4. 10.2.4. Getting a reference
      5. 10.2.5. Making data transient
      6. 10.2.6. Refreshing data
      7. 10.2.7. Replicating data
      8. 10.2.8. Caching in the persistence context
      9. 10.2.9. Flushing the persistence context
    3. 10.3. Working with detached state
      1. 10.3.1. The identity of detached instances
      2. 10.3.2. Implementing equality methods
      3. 10.3.3. Detaching entity instances
      4. 10.3.4. Merging entity instances
    4. 10.4. Summary
  21. Chapter 11. Transactions and concurrency
    1. 11.1. Transaction essentials
      1. 11.1.1. ACID attributes
      2. 11.1.2. Database and system transactions
      3. 11.1.3. Programmatic transactions with JTA
      4. 11.1.4. Handling exceptions
      5. 11.1.5. Declarative transaction demarcation
    2. 11.2. Controlling concurrent access
      1. 11.2.1. Understanding database-level concurrency
      2. 11.2.2. Optimistic concurrency control
      3. 11.2.3. Explicit pessimistic locking
      4. 11.2.4. Avoiding deadlocks
    3. 11.3. Nontransactional data access
      1. 11.3.1. Reading data in auto-commit mode
      2. 11.3.2. Queueing modifications
    4. 11.4. Summary
  22. Chapter 12. Fetch plans, strategies, and profiles
    1. 12.1. Lazy and eager loading
      1. 12.1.1. Understanding entity proxies
      2. 12.1.2. Lazy persistent collections
      3. 12.1.3. Lazy loading with interception
      4. 12.1.4. Eager loading of associations and collections
    2. 12.2. Selecting a fetch strategy
      1. 12.2.1. The n+1 selects problem
      2. 12.2.2. The Cartesian product problem
      3. 12.2.3. Prefetching data in batches
      4. 12.2.4. Prefetching collections with subselects
      5. 12.2.5. Eager fetching with multiple SELECTs
      6. 12.2.6. Dynamic eager fetching
    3. 12.3. Using fetch profiles
      1. 12.3.1. Declaring Hibernate fetch profiles
      2. 12.3.2. Working with entity graphs
    4. 12.4. Summary
  23. Chapter 13. Filtering data
    1. 13.1. Cascading state transitions
      1. 13.1.1. Available cascading options
      2. 13.1.2. Transitive detachment and merging
      3. 13.1.3. Cascading refresh
      4. 13.1.4. Cascading replication
      5. 13.1.5. Enabling global transitive persistence
    2. 13.2. Listening to and intercepting events
      1. 13.2.1. JPA event listeners and callbacks
      2. 13.2.2. Implementing Hibernate interceptors
      3. 13.2.3. The core event system
    3. 13.3. Auditing and versioning with Hibernate Envers
      1. 13.3.1. Enabling audit logging
      2. 13.3.2. Creating an audit trail
      3. 13.3.3. Finding revisions
      4. 13.3.4. Accessing historical data
    4. 13.4. Dynamic data filters
      1. 13.4.1. Defining dynamic filters
      2. 13.4.2. Applying the filter
      3. 13.4.3. Enabling the filter
      4. 13.4.4. Filtering collection access
    5. 13.5. Summary
  24. Part 4. Writing queries
  25. Chapter 14. Creating and executing queries
    1. 14.1. Creating queries
      1. 14.1.1. The JPA query interfaces
      2. 14.1.2. Typed query results
      3. 14.1.3. Hibernate’s query interfaces
    2. 14.2. Preparing queries
      1. 14.2.1. Protecting against SQL injection attacks
      2. 14.2.2. Binding named parameters
      3. 14.2.3. Using positional parameters
      4. 14.2.4. Paging through large result sets
    3. 14.3. Executing queries
      1. 14.3.1. Listing all results
      2. 14.3.2. Getting a single result
      3. 14.3.3. Scrolling with database cursors
      4. 14.3.4. Iterating through a result
    4. 14.4. Naming and externalizing queries
      1. 14.4.1. Calling a named query
      2. 14.4.2. Defining queries in XML metadata
      3. 14.4.3. Defining queries with annotations
      4. 14.4.4. Defining named queries programmatically
    5. 14.5. Query hints
      1. 14.5.1. Setting a timeout
      2. 14.5.2. Setting the flush mode
      3. 14.5.3. Setting read-only mode
      4. 14.5.4. Setting a fetch size
      5. 14.5.5. Setting an SQL comment
      6. 14.5.6. Named query hints
    6. 14.6. Summary
  26. Chapter 15. The query languages
    1. 15.1. Selection
      1. 15.1.1. Assigning aliases and query roots
      2. 15.1.2. Polymorphic queries
    2. 15.2. Restriction
      1. 15.2.1. Comparison expressions
      2. 15.2.2. Expressions with collections
      3. 15.2.3. Calling functions
      4. 15.2.4. Ordering query results
    3. 15.3. Projection
      1. 15.3.1. Projection of entities and scalar values
      2. 15.3.2. Using dynamic instantiation
      3. 15.3.3. Getting distinct results
      4. 15.3.4. Calling functions in projections
      5. 15.3.5. Aggregation functions
      6. 15.3.6. Grouping
    4. 15.4. Joins
      1. 15.4.1. Joins with SQL
      2. 15.4.2. Join options in JPA
      3. 15.4.3. Implicit association joins
      4. 15.4.4. Explicit joins
      5. 15.4.5. Dynamic fetching with joins
      6. 15.4.6. Theta-style joins
      7. 15.4.7. Comparing identifiers
    5. 15.5. Subselects
      1. 15.5.1. Correlated and uncorrelated nesting
      2. 15.5.2. Quantification
    6. 15.6. Summary
  27. Chapter 16. Advanced query options
    1. 16.1. Transforming query results
      1. 16.1.1. Returning a list of lists
      2. 16.1.2. Returning a list of maps
      3. 16.1.3. Mapping aliases to bean properties
      4. 16.1.4. Writing a ResultTransformer
    2. 16.2. Filtering collections
    3. 16.3. The Hibernate criteria query API
      1. 16.3.1. Selection and ordering
      2. 16.3.2. Restriction
      3. 16.3.3. Projection and aggregation
      4. 16.3.4. Joins
      5. 16.3.5. Subselects
      6. 16.3.6. Example queries
    4. 16.4. Summary
  28. Chapter 17. Customizing SQL
    1. 17.1. Falling back to JDBC
    2. 17.2. Mapping SQL query results
      1. 17.2.1. Projection with SQL queries
      2. 17.2.2. Mapping to an entity class
      3. 17.2.3. Customizing result mappings
      4. 17.2.4. Externalizing native queries
    3. 17.3. Customizing CRUD operations
      1. 17.3.1. Enabling custom loaders
      2. 17.3.2. Customizing creation, updates, and deletion
      3. 17.3.3. Customizing collection operations
      4. 17.3.4. Eager fetching in custom loaders
    4. 17.4. Calling stored procedures
      1. 17.4.1. Returning a result set
      2. 17.4.2. Returning multiple results and update counts
      3. 17.4.3. Setting input and output parameters
      4. 17.4.4. Returning a cursor
    5. 17.5. Using stored procedures for CRUD
      1. 17.5.1. Custom loader with a procedure
      2. 17.5.2. Procedures for CUD
    6. 17.6. Summary
  29. Part 5. Building applications
  30. Chapter 18. Designing client/server applications
    1. 18.1. Creating a persistence layer
      1. 18.1.1. A generic data access object pattern
      2. 18.1.2. Implementing the generic interface
      3. 18.1.3. Implementing entity DAOs
      4. 18.1.4. Testing the persistence layer
    2. 18.2. Building a stateless server
      1. 18.2.1. Editing an auction item
      2. 18.2.2. Placing a bid
      3. 18.2.3. Analyzing the stateless application
    3. 18.3. Building a stateful server
      1. 18.3.1. Editing an auction item
      2. 18.3.2. Analyzing the stateful application
    4. 18.4. Summary
  31. Chapter 19. Building web applications
    1. 19.1. Integrating JPA with CDI
      1. 19.1.1. Producing an EntityManager
      2. 19.1.2. Joining the EntityManager with transactions
      3. 19.1.3. Injecting an EntityManager
    2. 19.2. Paging and sorting data
      1. 19.2.1. Offset paging vs. seeking
      2. 19.2.2. Paging in the persistence layer
      3. 19.2.3. Querying page-by-page
    3. 19.3. Building JSF applications
      1. 19.3.1. Request-scoped services
      2. 19.3.2. Conversation-scoped services
    4. 19.4. Serializing domain model data
      1. 19.4.1. Writing a JAX-RS service
      2. 19.4.2. Applying JAXB mappings
      3. 19.4.3. Serializing Hibernate proxies
    5. 19.5. Summary
  32. Chapter 20. Scaling Hibernate
    1. 20.1. Bulk and batch processing
      1. 20.1.1. Bulk statements in JPQL and criteria
      2. 20.1.2. Bulk statements in SQL
      3. 20.1.3. Processing in batches
      4. 20.1.4. The Hibernate StatelessSession interface
    2. 20.2. Caching data
      1. 20.2.1. The Hibernate shared cache architecture
      2. 20.2.2. Configuring the shared cache
      3. 20.2.3. Enabling entity and collection caching
      4. 20.2.4. Testing the shared cache
      5. 20.2.5. Setting cache modes
      6. 20.2.6. Controlling the shared cache
      7. 20.2.7. The query result cache
    3. 20.3. Summary
  33. Appendix. References
  34. Index
    1. SYMBOL
    2. A
    3. B
    4. C
    5. D
    6. E
    7. F
    8. G
    9. H
    10. I
    11. J
    12. K
    13. L
    14. M
    15. N
    16. O
    17. P
    18. Q
    19. R
    20. S
    21. T
    22. U
    23. V
    24. W
    25. X
    26. Y
  35. List of Figures
  36. List of Tables
  37. List of Listings

Product information

  • Title: Java Persistence with Hibernate, Second Edition
  • Author(s): Christian Bauer, Gary Gregory
  • Release date: October 2015
  • Publisher(s): Manning Publications
  • ISBN: 9781617290459