You are previewing Spring Persistence with Hibernate.
O'Reilly logo
Spring Persistence with Hibernate

Book Description

Persistence is an important set of techniques and technologies for accessing and transacting data, and ensuring that data is mobile regardless of specific applications and contexts. In Java development, persistence is a key factor in enterprise, e-commerce, and other transaction-oriented applications.

Today, the Spring Framework is the leading out-of-the-box solution for enterprise Java developers; in it, you can find a number of Java Persistence solutions.

This book gets you rolling with fundamental Spring Framework 3 concepts and integrating persistence functionality into enterprise Java applications using Hibernate, the Java™ Persistence API (JPA) 2, and the Grails Object Relational Mapping tool, GORM.

  • Covers core Hibernate fundamentals, demonstrating how the framework can be best utilized within a Spring application context

  • Covers how to use and integrate JPA 2, found in the new Java EE 6 platform

  • Covers how to integrate and use the new Grails persistence engine, GORM

Table of Contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Preface
  6. 1. Architecting Your Application with Spring, Hibernate, and Patterns
    1. 1.1. The Benefit of a Consistent Approach
      1. 1.1.1. The Significance of Dependency Injection
    2. 1.2. A Synergistic Partnership
      1. 1.2.1. The Story of Spring's and Hibernate's Success
      2. 1.2.2. A Better Approach for Integration
      3. 1.2.3. Best Practices for Architecting an Application
        1. 1.2.3.1. The Layers of a Persistence Tier
          1. 1.2.3.1.1. The Domain Model
          2. 1.2.3.1.2. The Data Access Object (DAO) Layer
          3. 1.2.3.1.3. The Service Facade
        2. 1.2.3.2. Leveraging Declarative Transactions
          1. 1.2.3.2.1. Understanding Aspect Oriented Programming (AOP)
          2. 1.2.3.2.2. Simplifying Transactions
          3. 1.2.3.2.3. The Benefit of Coding to Interfaces
        3. 1.2.3.3. Advanced Features and Performance Tuning
          1. 1.2.3.3.1. Hibernate-Search
          2. 1.2.3.3.2. Building a REST Web Service
    3. 1.3. Other Persistence Design Patterns
      1. 1.3.1. The Template Pattern
      2. 1.3.2. The Active-Record Pattern
    4. 1.4. Summary
  7. 2. Spring Basics
    1. 2.1. Exploring Spring's Architecture
      1. 2.1.1. The Application Context
      2. 2.1.2. Beans, Beans, the Magical Fruit
      3. 2.1.3. The Spring Life Cycle
      4. 2.1.4. Understanding Bean Scopes
    2. 2.2. Dependency Injection and Inversion of Control
      1. 2.2.1. Setter-Based Dependency Injection
      2. 2.2.2. Constructor-Based Dependency Injection
      3. 2.2.3. Instance Collaboration
      4. 2.2.4. Coding to Interfaces
      5. 2.2.5. Dependency Injection via Autowiring
      6. 2.2.6. @Annotation-Based Dependency Injection
      7. 2.2.7. Set It and Forget It!
    3. 2.3. Injecting Code Using AOP and Interceptors
    4. 2.4. Summary
  8. 3. Basic Application Setup
    1. 3.1. Application Management with Maven
      1. 3.1.1. Managed Dependencies
      2. 3.1.2. Standard Directory Structure
      3. 3.1.3. POM Deconstruction
    2. 3.2. Spring Configuration
      1. 3.2.1. Namespace Support
      2. 3.2.2. Externalizing Property Configurations
      3. 3.2.3. Component Scanning
      4. 3.2.4. Import Statements
    3. 3.3. Database Integration
      1. 3.3.1. JDBC Support
      2. 3.3.2. Integration with JNDI
    4. 3.4. Web Application Configuration
      1. 3.4.1. Servlet Definition
      2. 3.4.2. Spring MVC
    5. 3.5. Summary
  9. 4. Persistence with Hibernate
    1. 4.1. The Evolution of Database Persistence in Java
      1. 4.1.1. EJB, JDO, and JPA
      2. 4.1.2. How Hibernate Fits In
    2. 4.2. JPA Interface Hierarchy
    3. 4.3. The Art Gallery Domain Model and DAO Structure
      1. 4.3.1. An @Entity-Annotated POJO
      2. 4.3.2. Simplified DAO Pattern with Generics
      3. 4.3.3. The Life Cycle of a JPA Entity
    4. 4.4. JPA Configuration
      1. 4.4.1. Bare-Bones JPA Setup
      2. 4.4.2. Spring Integration
    5. 4.5. Summary
  10. 5. Domain Model Fundamentals
    1. 5.1. Understanding Associations
    2. 5.2. Building the Domain Model
      1. 5.2.1. Convention over Configuration
      2. 5.2.2. Managing Entity Identifiers
      3. 5.2.3. Using Cascading Options to Establish Data Relationships
      4. 5.2.4. Adding Second-Level Caching
      5. 5.2.5. Using Polymorphism with Hibernate
    3. 5.3. Summary
  11. 6. DAOs and Querying
    1. 6.1. A Basic Hibernate DAO Implementation
      1. 6.1.1. Building a DAO
      2. 6.1.2. Using Spring's Hibernate Support Classes
      3. 6.1.3. Enabling Query Caching with the HibernateTemplate
      4. 6.1.4. Going Template-less
    2. 6.2. Querying in Hibernate
      1. 6.2.1. Loading an Entity
      2. 6.2.2. Querying for a Particular Type
      3. 6.2.3. Using Named Parameters
      4. 6.2.4. Querying Using Core Hibernate
      5. 6.2.5. Using Named Queries
      6. 6.2.6. Working with Polymorphic Queries
    3. 6.3. Persisting Data with Hibernate
      1. 6.3.1. Saving and Updating Data
      2. 6.3.2. Handling Binary Data
      3. 6.3.3. Understanding the Benefits of the Criteria API
      4. 6.3.4. Using the JPA 2.0 Criteria API
    4. 6.4. Summary
  12. 7. Transaction Management
    1. 7.1. The Joy of ACID
    2. 7.2. Understanding Isolation Levels
      1. 7.2.1. Serializable
      2. 7.2.2. Repeatable Read
      3. 7.2.3. Read Committed
      4. 7.2.4. Read Uncommitted
    3. 7.3. Controlling ACID Reflux
      1. 7.3.1. Platform Transaction Management
      2. 7.3.2. Declarative Transaction Management
        1. 7.3.2.1. Transactional Annotations
        2. 7.3.2.2. Declarative Transactions via XML
      3. 7.3.3. Programmatic Transaction Management
    4. 7.4. Transactional Examples
      1. 7.4.1. Creating a Batch Application
      2. 7.4.2. Using Two Datasources
    5. 7.5. Summary
  13. 8. Effective Testing
    1. 8.1. Unit, Integration, and Functional Testing
    2. 8.2. Using JUnit for Effective Testing
      1. 8.2.1. Unit Testing with Mocks
      2. 8.2.2. Spring Dependency Injection and Testing
      3. 8.2.3. Testing with a Database
    3. 8.3. Summary
  14. 9. Best Practices and Advanced Techniques
    1. 9.1. Lazy Loading Issues
      1. 9.1.1. The N+1 Selects Problem
        1. 9.1.1.1. Less Lazy Mappings
        2. 9.1.1.2. Batching for Performance
      2. 9.1.2. Lazy Initialization Exceptions
        1. 9.1.2.1. Now Open Late: Keeping EntityManager Open Past Its Bedtime
        2. 9.1.2.2. Applying the Open EntityManager Filter
    2. 9.2. Caching
      1. 9.2.1. Integrating a Caching Implementation
        1. 9.2.1.1. Determining Caching Rules
        2. 9.2.1.2. Configuring Cache Regions
      2. 9.2.2. Caching Your Queries
      3. 9.2.3. Caching in a Clustered Configuration
        1. 9.2.3.1. Cluster Caching and Replication Mechanics
        2. 9.2.3.2. Configuring Replication
    3. 9.3. Summary
  15. 10. Integration Frameworks
    1. 10.1. RESTful Web Services with Spring
      1. 10.1.1. Nouns, Verbs, and Content-Types
      2. 10.1.2. Serializing the Object Graph
      3. 10.1.3. Using the Dreaded DTO Pattern
        1. 10.1.3.1. Bootstrapping Dozer
        2. 10.1.3.2. Building the DTO Layer
        3. 10.1.3.3. Configuring Dozer with Spring
        4. 10.1.3.4. Making the Mapping Happen
      4. 10.1.4. Leveraging Spring 3's REST Support
      5. 10.1.5. Marshaling Data with Spring OXM
      6. 10.1.6. Handling Concurrency
        1. 10.1.6.1. Optimistic Locking
        2. 10.1.6.2. Pessimistic Locking
    2. 10.2. Free-Text Search
      1. 10.2.1. Introducing Lucene
        1. 10.2.1.1. Indexing with Lucene
        2. 10.2.1.2. Querying with Lucene
      2. 10.2.2. Introducing Hibernate Search
        1. 10.2.2.1. Integrating Hibernate Search
        2. 10.2.2.2. Adding Hibernate Search Annotations
      3. 10.2.3. Putting Lucene and Hibernate in Sync
      4. 10.2.4. Building a Domain-Specific Search
    3. 10.3. Summary
  16. 11. GORM and Grails
    1. 11.1. A Crash Course in Groovy
      1. 11.1.1. Letting Your Types Loose
      2. 11.1.2. GStrings—Strings on Steroids
      3. 11.1.3. Default Constructors in Groovy
      4. 11.1.4. Closures in Groovy
    2. 11.2. Getting Grails Running
      1. 11.2.1. Installing Grails
      2. 11.2.2. Creating a Grails Application
      3. 11.2.3. Configuring Your Application
      4. 11.2.4. Configuring Your Datasource
      5. 11.2.5. Mapping URLs
    3. 11.3. Defining the Grails Domain Model
      1. 11.3.1. Adding Constraints and Validation
      2. 11.3.2. Defining Associations and Properties
      3. 11.3.3. Customizing Domain Class Hibernate Mappings
    4. 11.4. Using Active Record As an Alternative to DAOs
    5. 11.5. Looking Under the Hood of GORM
      1. 11.5.1. Working with Dynamic Finder Methods
      2. 11.5.2. Creating Advanced Query Methods
      3. 11.5.3. Using the Criteria API
    6. 11.6. Handling Associations in Grails
    7. 11.7. Scaffolding and Building Your Grails Application
    8. 11.8. Defining a Transactional Service Layer in Grails
    9. 11.9. Summary
  17. 12. Spring Roo
    1. 12.1. What Roo Is (and What It Is Not)
    2. 12.2. Creating a Domain Model with Roo
      1. 12.2.1. Getting Started with Roo
      2. 12.2.2. Creating a New Project
      3. 12.2.3. Adding Entities
      4. 12.2.4. Adding Fields
      5. 12.2.5. Exploring the Automatically Generated Testing Infrastructure
      6. 12.2.6. Mapping Associations
      7. 12.2.7. Modeling Inheritance
      8. 12.2.8. Adding Spring MVC
      9. 12.2.9. Adding Service Layers and DAOs
    3. 12.3. Now You See Me, Now You Don't—Removing Roo
    4. 12.4. Summary