Hibernate Recipes: A Problem-Solution Approach

Book description

Hibernate continues to be the most popular out-of-the-box framework solution for Java Persistence and data/database accessibility techniques and patterns. It is used for e-commerce–based web applications as well as heavy-duty transactional systems for the enterprise.

Gary Mak, the author of the best-selling Spring Recipes, now brings you Hibernate Recipes. This book contains a collection of code recipes and templates for learning and building Hibernate solutions for you and your clients.

This book is your pragmatic day-to-day reference and guide for doing all things involving Hibernate. There are many books focused on learning Hibernate, but this book takes you further and shows how you can apply it practically in your daily work.

Table of contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgments
  5. 1. Starting with Hibernate
    1. 1.1. Setting Up Hibernate
      1. 1.1.1. Problem
      2. 1.1.2. Solution
      3. 1.1.3. How It Works
        1. 1.1.3.1. Installing the JDK
        2. 1.1.3.2. Installing the Eclipse Web Tools Platform (WTP)
        3. 1.1.3.3. Installing Derby
          1. 1.1.3.3.1. Creating a Derby Database Instance
          2. 1.1.3.3.2. Creating the Tables (Relational Model)
    2. 1.2. Programming with Basic JDBC
      1. 1.2.1. Problem
      2. 1.2.2. Solution
      3. 1.2.3. How It Works
        1. 1.2.3.1. Creating an Eclipse Project
        2. 1.2.3.2. JDBC Initialization and Cleanup
        3. 1.2.3.3. Using JDBC to Query a Database
        4. 1.2.3.4. Using JDBC to Update a Database
        5. 1.2.3.5. Creating the Domain Model
        6. 1.2.3.6. Retrieving Object Graphs
        7. 1.2.3.7. Persisting Object Graphs
        8. 1.2.3.8. Problems with Using JDBC
    3. 1.3. Configuring Hibernate
      1. 1.3.1. Problem
      2. 1.3.2. Solution
      3. 1.3.3. How It Works
        1. 1.3.3.1. Getting the Required Jars
        2. 1.3.3.2. Creating Mapping Definitions
        3. 1.3.3.3. Configuration
        4. 1.3.3.4. Programmatic Configuration
          1. 1.3.3.4.1. SessionFactory
        5. 1.3.3.5. XML Configuration
        6. 1.3.3.6. Opening and Closing Sessions
        7. 1.3.3.7. Retrieving Objects
    4. 1.4. Configuring a JPA Project
      1. 1.4.1. Problem
      2. 1.4.2. Solution
      3. 1.4.3. How It Works
        1. 1.4.3.1. Opening a Session
    5. 1.5. Using Entity Manager
      1. 1.5.1. Problem
      2. 1.5.2. Solution
      3. 1.5.3. How It Works
    6. 1.6. Enabling Logging in Hibernate
      1. 1.6.1. Problem
      2. 1.6.2. Solution
      3. 1.6.3. How It Works
        1. 1.6.3.1. Inspecting the SQL Statements Issued by Hibernate
        2. 1.6.3.2. Configuring Log4j
        3. 1.6.3.3. Enabling Live Statistics
    7. 1.7. Generating a Database Schema Using Hibernate
      1. 1.7.1. Problem
      2. 1.7.2. Solution
      3. 1.7.3. How It Works
        1. 1.7.3.1. Creating an Ant Build File
        2. 1.7.3.2. Generating Database Schema Using SchemaExport
        3. 1.7.3.3. Updating a Database Schema Using SchemaUpdate
        4. 1.7.3.4. Specifying the Details of a Database Schema
    8. 1.8. Summary
  6. 2. Basic Mapping and Object Identity
    1. 2.1. Providing an ID for Persistence
      1. 2.1.1. Problem
      2. 2.1.2. Solution
      3. 2.1.3. How It Works
        1. 2.1.3.1. Hibernate XML Mapping
          1. 2.1.3.1.1. Using the Database Sequence
          2. 2.1.3.1.2. Using a Native Generator
          3. 2.1.3.1.3. Using an Increment Generator
          4. 2.1.3.1.4. Using the Hilo Generator
        2. 2.1.3.2. Using JPA to Generate Identifiers
    2. 2.2. Creating a Composite Key in Hibernate
      1. 2.2.1. Problem
      2. 2.2.2. Solution
      3. 2.2.3. How It Works
    3. 2.3. SaveOrUpdate in Hibernate
      1. 2.3.1. Problem
      2. 2.3.2. Solution
      3. 2.3.3. How It Works
    4. 2.4. Dynamic SQL Generation in Hibernate
      1. 2.4.1. Problem
      2. 2.4.2. Solution
      3. 2.4.3. How It Works
    5. 2.5. Naming Entities in Hibernate
      1. 2.5.1. Problem
      2. 2.5.2. Solution
      3. 2.5.3. How It Works
    6. 2.6. Summary
  7. 3. Component Mapping
    1. 3.1. Implementing a Value Type as a Component
      1. 3.1.1. Problem
      2. 3.1.2. Solution
      3. 3.1.3. How It Works
        1. 3.1.3.1. Using Hibernate XML Mapping
      4. 3.1.4. Using JPA Annotations
    2. 3.2. Nesting Components
      1. 3.2.1. Problem
      2. 3.2.2. Solution
      3. 3.2.3. How It Works
    3. 3.3. Adding References in Components
      1. 3.3.1. Problem
      2. 3.3.2. Solution
      3. 3.3.3. How It Works
    4. 3.4. Mapping a Collection of Components
      1. 3.4.1. Problem
      2. 3.4.2. Solution
      3. 3.4.3. How It Works
    5. 3.5. Using Components as Keys to a Map
      1. 3.5.1. Problem
      2. 3.5.2. Solution
      3. 3.5.3. How It Works
    6. 3.6. Summary
  8. 4. Inheritance and Custom Mapping
    1. 4.1. Mapping Entities with Table per Class Hierarchy
      1. 4.1.1. Problem
      2. 4.1.2. Solution
      3. 4.1.3. How It Works
    2. 4.2. Mapping Entities with Table per Subclass
      1. 4.2.1. Problem
      2. 4.2.2. Solution
      3. 4.2.3. How It Works
    3. 4.3. Mapping Entities with Table per Concrete Class
      1. 4.3.1. Problem
      2. 4.3.2. Solution
      3. 4.3.3. How It Works
    4. 4.4. Custom Mappings
      1. 4.4.1. Problem
      2. 4.4.2. Solution
      3. 4.4.3. How It Works
    5. 4.5. CompositeUserType Mappings
      1. 4.5.1. Problem
      2. 4.5.2. Solution
      3. 4.5.3. How It Works
    6. 4.6. Summary
  9. 5. Many-to-One and One-to-One Mapping
    1. 5.1. Using Many-To-One Associations
      1. 5.1.1. Problem
      2. 5.1.2. Solution
      3. 5.1.3. How It Works
    2. 5.2. Using a Many-to-One Association with a Join Table
      1. 5.2.1. Problem
      2. 5.2.2. Solution
      3. 5.2.3. How It Works
    3. 5.3. Using Lazy Initialization on Many-to-One Associations
      1. 5.3.1. Problem
      2. 5.3.2. Solution
      3. 5.3.3. How It Works
    4. 5.4. Sharing Primary Key Associations
      1. 5.4.1. Problem
      2. 5.4.2. Solution
      3. 5.4.3. How It Works
    5. 5.5. Creating a One-to-One Association Using a Foreign Key
      1. 5.5.1. Problem
      2. 5.5.2. Solution
      3. 5.5.3. How It Works
    6. 5.6. Creating a One-to-One Association Using a Join Table
      1. 5.6.1. Problem
      2. 5.6.2. Solution
      3. 5.6.3. How It Works
    7. 5.7. Summary
  10. 6. Collection Mapping
    1. 6.1. Mapping a Set
      1. 6.1.1. Problem
      2. 6.1.2. Solution
      3. 6.1.3. How It Works
    2. 6.2. Mapping a Bag
      1. 6.2.1. Problem
      2. 6.2.2. Solution
      3. 6.2.3. How It Works
    3. 6.3. Mapping a List
      1. 6.3.1. Problem
      2. 6.3.2. Solution
      3. 6.3.3. How It Works
    4. 6.4. Mapping an Array
      1. 6.4.1. Problem
      2. 6.4.2. Solution
      3. 6.4.3. How It Works
    5. 6.5. Mapping a Map
      1. 6.5.1. Problem
      2. 6.5.2. Solution
      3. 6.5.3. How It Works
    6. 6.6. Sorting Collections
      1. 6.6.1. Problem
      2. 6.6.2. Solution
      3. 6.6.3. How It Works
        1. 6.6.3.1. Using the Natural Order
        2. 6.6.3.2. Writing Your Own Comparator
        3. 6.6.3.3. Sorting in the Database
    7. 6.7. Using Lazy Initialization
      1. 6.7.1. Problem
      2. 6.7.2. Solution
      3. 6.7.3. How It Works
    8. 6.8. Summary
  11. 7. Many-Valued Associations
    1. 7.1. Mapping a One-to-Many Association with a Foreign Key
      1. 7.1.1. Problem
      2. 7.1.2. Solution
      3. 7.1.3. How It Works
    2. 7.2. Mapping a One-to-Many Bidirectional Association Using a Foreign Key
      1. 7.2.1. Problem
      2. 7.2.2. Solution
      3. 7.2.3. How It Works
    3. 7.3. Mapping a One-to-Many Bidirectional Association Using a Join Table
      1. 7.3.1. Problem
      2. 7.3.2. Solution
      3. 7.3.3. How It Works
    4. 7.4. Mapping a Many-to-Many Unidirectional Association with a Join Table
      1. 7.4.1. Problem
      2. 7.4.2. Solution
      3. 7.4.3. How It Works
    5. 7.5. Creating a Many-to-Many Bidirectional Association with a Join Table
      1. 7.5.1. Problem
      2. 7.5.2. Solution
      3. 7.5.3. How It Works
    6. 7.6. Summary
  12. 8. HQL and JPA Query Language
    1. 8.1. Using the Query Object
      1. 8.1.1. Problem
      2. 8.1.2. Solution
      3. 8.1.3. How It Works
        1. 8.1.3.1. Creating a Query Object
        2. 8.1.3.2. The from Clause
        3. 8.1.3.3. The where Clause
        4. 8.1.3.4. Pagination
        5. 8.1.3.5. Parameter Binding
        6. 8.1.3.6. Named Queries
    2. 8.2. Using the Select Clause
      1. 8.2.1. Problem
      2. 8.2.2. Solution
      3. 8.2.3. How It Works
    3. 8.3. Joining
      1. 8.3.1. Problem
      2. 8.3.2. Solution
      3. 8.3.3. How It Works
        1. 8.3.3.1. Explicit Joins
        2. 8.3.3.2. Implicit Joins
        3. 8.3.3.3. Outer Joins
        4. 8.3.3.4. Matching Text
        5. 8.3.3.5. Fetching Associations
    4. 8.4. Creating Report Queries
      1. 8.4.1. Problem
      2. 8.4.2. Solution
      3. 8.4.3. How It Works
        1. 8.4.3.1. Projection with Aggregation Functions
        2. 8.4.3.2. Grouping Aggregated Results
    5. 8.5. Summary
  13. 9. Querying with Criteria and Example
    1. 9.1. Using Criteria
      1. 9.1.1. Problem
      2. 9.1.2. Solution
      3. 9.1.3. How It Works
    2. 9.2. Using Restrictions
      1. 9.2.1. Problem
      2. 9.2.2. Solution
      3. 9.2.3. How It Works
        1. 9.2.3.1. Writing Subqueries
    3. 9.3. Using Criteria in Associations
      1. 9.3.1. Problem
      2. 9.3.2. Solution
      3. 9.3.3. How It Works
    4. 9.4. Using Projections
      1. 9.4.1. Problem
      2. 9.4.2. Solution
      3. 9.4.3. How It Works
        1. 9.4.3.1. Aggregate Functions and Groupings with Projections
    5. 9.5. Querying by Example
      1. 9.5.1. Problem
      2. 9.5.2. Solution
      3. 9.5.3. How It Works
    6. 9.6. Summary
  14. 10. Working with Objects
    1. 10.1. Identifying Persistent Object States
      1. 10.1.1. Problem
      2. 10.1.2. Solution
      3. 10.1.3. How It Works
        1. 10.1.3.1. Transient Objects
        2. 10.1.3.2. Persistent Objects
        3. 10.1.3.3. Detached Objects
        4. 10.1.3.4. Removed Objects
    2. 10.2. Working with Persistent Objects
      1. 10.2.1. Problem
      2. 10.2.2. Solution
      3. 10.2.3. How It Works
        1. 10.2.3.1. Creating a Persistent Object
        2. 10.2.3.2. Retrieving a Persistent Object
        3. 10.2.3.3. Modifying a Persistent Object
        4. 10.2.3.4. Deleting a Persistent Object
    3. 10.3. Persisting Detached Objects
      1. 10.3.1. Problem
      2. 10.3.2. Solution
      3. 10.3.3. How It Works
        1. 10.3.3.1. Reattaching a Detached Object
        2. 10.3.3.2. Merging a Detached Object
    4. 10.4. Using Data Filters
      1. 10.4.1. Problem
      2. 10.4.2. Solution
      3. 10.4.3. How It Works
    5. 10.5. Using Interceptors
      1. 10.5.1. Problem
      2. 10.5.2. Solution
      3. 10.5.3. How It Works
    6. 10.6. Summary
  15. 11. Batch Processing and Native SQL
    1. 11.1. Performing Batch Inserts
      1. 11.1.1. Problem
      2. 11.1.2. Solution
      3. 11.1.3. How It Works
    2. 11.2. Performing Batch Updates and Deletes
      1. 11.2.1. Problem
      2. 11.2.2. Solution
      3. 11.2.3. How It Works
    3. 11.3. Using Native SQL
      1. 11.3.1. Problem
      2. 11.3.2. Solution
      3. 11.3.3. How It Works
    4. 11.4. Using Named SQL Queries
      1. 11.4.1. Problem
      2. 11.4.2. Solution
      3. 11.4.3. How It Works
    5. 11.5. Summary
  16. 12. Cashing in Hibernate
    1. 12.1. Using the Second-Level Cache in Hibernate
    2. 12.2. Concurrency Strategies
    3. 12.3. Cache Providers
    4. 12.4. What Are Cache Regions?
    5. 12.5. Caching Query Results
    6. 12.6. Using the First-Level Cache
      1. 12.6.1. Problem
      2. 12.6.2. Solution
      3. 12.6.3. How It Works
    7. 12.7. Configuring the Second-Level Cache
      1. 12.7.1. Problem
      2. 12.7.2. Solution
      3. 12.7.3. How It Works
    8. 12.8. Caching Associations
      1. 12.8.1. Problem
      2. 12.8.2. Solution
      3. 12.8.3. How It Works
    9. 12.9. Caching Collections
      1. 12.9.1. Problem
      2. 12.9.2. Solution
      3. 12.9.3. How It Works
    10. 12.10. Caching Queries
      1. 12.10.1. Problem
      2. 12.10.2. Solution
      3. 12.10.3. How It Works
    11. 12.11. Summary
  17. 13. Transactions and Concurrency
    1. 13.1. Using Programmatic Transactions in a Standalone Java Application
      1. 13.1.1. Problem
      2. 13.1.2. Solution
      3. 13.1.3. How It Works
    2. 13.2. Using Programmatic Transactions with JTA
      1. 13.2.1. Problem
      2. 13.2.2. Solution
      3. 13.2.3. How It Works
    3. 13.3. Enabling Optimistic Concurrency Control
      1. 13.3.1. Problem
      2. 13.3.2. Solution
      3. 13.3.3. How It Works
    4. 13.4. Using Pessimistic Concurrency Control
      1. 13.4.1. Problem
      2. 13.4.2. Solution
      3. 13.4.3. How It Works
    5. 13.5. Summary
  18. 14. Web Applications
    1. 14.1. Creating a Controller for the Bookshop Web Application
      1. 14.1.1. Problem
      2. 14.1.2. Solution
      3. 14.1.3. How It Works
        1. 14.1.3.1. Creating a Dynamic Web Project
        2. 14.1.3.2. Configuring the Connection Pool
        3. 14.1.3.3. Developing an Online Bookshop
        4. 14.1.3.4. Creating a Global Session Factory
        5. 14.1.3.5. Listing Persistent Objects
        6. 14.1.3.6. Updating Persistent Objects
        7. 14.1.3.7. Creating Persistent Objects
        8. 14.1.3.8. Deleting Persistent Objects
    2. 14.2. Creating a Data-Access Layer
      1. 14.2.1. Problem
      2. 14.2.2. Solution
      3. 14.2.3. How It Works
        1. 14.2.3.1. Organizing Data Access in Data-Access Objects
        2. 14.2.3.2. Using Generic Data-Access Objects
        3. 14.2.3.3. Using a Factory to Centralize DAO Retrieval
        4. 14.2.3.4. Navigating Lazy Associations
        5. 14.2.3.5. Using the Open Session in View Pattern
    3. 14.3. Summary

Product information

  • Title: Hibernate Recipes: A Problem-Solution Approach
  • Author(s): SRINIVAS GURUZU, GARY MAK
  • Release date: July 2010
  • Publisher(s): Apress
  • ISBN: 9781430227960