O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Spring Persistence with Hibernate

Book Description

Overview of Spring Persistence with Hibernate

  • Get to grips with Hibernate and its configuration manager, mappings, types, session APIs, queries, and much more

  • Integrate Hibernate and Spring as part of your enterprise Java stack development

  • Work with Spring IoC (Inversion of Control), Spring AOP, transaction management, web development, and unit testing considerations and features

  • Covers advanced and useful features of Hibernate in a practical way

In Detail

Spring is the leading platform to build and run enterprise Java applications. Spring's Hibernate integration makes it easy to mix and match persistence methodologies simplifying your Hibernate applications. You can incorporate lots of Inversion of Control (IoC) convenience features to address many typical Hibernate integration issues, making this integration all the more favorable for your application.

This easy-to-use book will turn the complex-sounding integration into a straightforward walk-through. Persistence is important for creating a data access-based transactions tier, central to financial, insurance, and banking applications. You will be able to enhance your applications using the most common, advanced, and optional features of Hibernate.

This book starts with the philosophy and the brief history of persistence. It provides an introduction to how persistence frameworks and technologies came into the development scene and what problems they are aimed to solve.

The book continues with a discussion about Hibernate as the most popular persistence framework in Java. First, you will learn how to get Hibernate and add it to a project and how to configure it before it can be used. Next, you will get an in-depth knowledge about Hibernate and understand the essential concepts behind persistence with Hibernate and more. When Hibernate has been fully discussed, you will get to know Spring as another popular framework in Java, and have a look at essential features of Spring and its added value for Hibernate-based projects. Finally the book will provide a comprehensive discussion about using Hibernate with Spring and the problems that are solved with Spring.

This book is a practical guide to learn Hibernate and its features, and integrate it with Spring, discussing how this integration can improve code development, testing, and maintenance.

Table of Contents

  1. Spring Persistence with Hibernate
    1. Spring Persistence with Hibernate
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Errata
        2. Piracy
        3. Questions
    6. 1. An Introduction to Hibernate and Spring
      1. Persistence management in Java
        1. The mismatch problem
          1. Identity and equality mismatch
          2. Mapping object inheritance mismatch
          3. Mapping more complicated objects
            1. Mapping a many-to-many association
      2. Object relational mapping
      3. Hibernate as an O/R Mapping solution
        1. Other O/R Mapping solutions
        2. Hibernate versus other frameworks
      4. Hibernate architecture
      5. What is Spring
        1. Inversion of Control container
        2. Aspect-oriented programming framework
        3. Data access abstraction
        4. Transaction abstraction
        5. MVC web framework
        6. Testing support
      6. Summary
    7. 2. Preparing an Application to Use Spring with Hibernate
      1. Setting up the database
        1. Getting and installing HSQLDB
        2. Configuring and running HSQLDB
        3. HSQLDB server modes
        4. Managing HSQLDB
      2. Getting a Hibernate distribution
      3. Getting a Spring distribution
      4. Setting up the project hierarchy
        1. Put the required libraries in the lib directory
      5. Setting up Ant or Maven in the project
      6. Summary
    8. 3. A Quick Tour of Hibernate and Spring
      1. Getting started with Hibernate
        1. Designing and implementing persistent classes
        2. Creating Database Tables
        3. Creating mapping metadata
        4. A simple client
          1. Configuring Hibernate
          2. Obtaining a session object
          3. Starting a transaction
          4. Performing the operation
          5. Committing/rolling back the transaction
      2. Hibernate declarative configuration
      3. Some issues in mapping
        1. Caching
        2. Querying objects
      4. Getting started with Spring
        1. A simple case
        2. Applying IoC
          1. Remove object instantiation and implement the setter method
          2. Configure the Student object
          3. Obtain the Student instance from Spring
      5. Hibernate with Spring
      6. Summary
    9. 4. Hibernate Configuration
      1. Basic configuration information
      2. Hibernate dialect
      3. Configuring Hibernate
        1. Programmatic configuration
        2. Declarative configuration
          1. Using a properties file
          2. Using an XML file
      4. Using a single instance of SessionFactory
      5. JPA configuration
      6. Summary
    10. 5. Hibernate Mappings
      1. Persistent entity classes
        1. Having a zero-argument constructor (mandatory)
        2. Providing accessors to access class properties (optional)
        3. Defining nonfinal classes (optional)
        4. Implementing equals() and hashCode() (optional)
      2. Object/relational mapping metadata
        1. Metadata in XML
          1. Doctype
          2. The <hibernate-mapping> root element
          3. The <class> element
          4. The <id> element
          5. The <property> element
            1. The formula attribute
            2. The insert and update attributes
        2. Metadata in annotations
          1. @Entity
          2. @Table
          3. @Id and @GeneratedValue
          4. @Basic
          5. @Lob
          6. @Transient
          7. @Column
      3. Mapping inheritance hierarchy
        1. One table for each concrete class
        2. One table for class hierarchy
        3. One table per subclass
        4. Implicit polymorphism versus explicit polymorphism
      4. Summary
    11. 6. More on Mappings
      1. Mapping components
      2. Mapping collections
        1. The <set> element
        2. The <bag> element
        3. The <idbag> element
        4. The <list> element
        5. The <map> element
        6. Mapping collections with annotations
        7. Sorted sets and sorted maps
        8. Using the order-by attribute to order collection elements
      3. Mapping object associations
        1. The <one-to-one> element
          1. Using identical primary keys
          2. Foreign key one-to-one
        2. The <many-to-one> element
        3. The <one-to-many> element
          1. Mapping a one-to-many relationship with other collections
            1. Mapping a set of entity types
            2. Mapping a collection of entity types with <idbag>
            3. Mapping a list of entity types
            4. Mapping a java.util.Map of entity types
        4. The <many-to-many> element
      4. Summary
    12. 7. Hibernate Types
      1. Built-in types
      2. Custom types
        1. UserType
        2. CompositeUserType
      3. Summary
    13. 8. Hibernate Persistence Behavior
      1. The life cycle of persistent objects
        1. Transient objects
        2. Persistent objects
        3. Detached objects
        4. Removed objects
      2. Persistence with Hibernate
        1. The process of persistence
        2. Storing objects
        3. Object equality and identity
        4. Loading objects
        5. Refreshing objects
        6. Updating objects
          1. Checking for dirty Sessions
          2. Using the merge() method
        7. Deleting objects
        8. Replicating objects
      3. Cascading operations
        1. An example cascading operation
          1. Using cascade="save-update"
          2. Using cascade="none"
          3. Using cascade="delete"
          4. Using cascade="delete-orphan"
      4. Lazy loading
      5. Some useful Session methods
      6. Summary
    14. 9. Querying In Hibernate
      1. The Session API and querying
      2. HQL
        1. The from clause
        2. The as clause
        3. Query an object's associations
        4. The select clause
        5. HQL's aggregate functions
        6. The where clause
        7. Positional versus named parameters
        8. The order by and group by clauses
      3. Bulk updates and bulk deletes with HQL
      4. Queries in native SQL
      5. Named SQL and HQL queries
      6. Using the Criteria API
        1. Using a simple Criteria
        2. Looking at the Restrictions class's factory methods
          1. Equality restrictions
          2. Null and empty restrictions
          3. Likeness restrictions
          4. Comparison restrictions
          5. Logical restrictions
          6. Size restrictions
          7. Disjunctions and conjunctions
          8. SQL restrictions
        3. Query By Example (QBE)
      7. Paging the query result
      8. Logging the Hibernate-Generated SQL
      9. Summary
    15. 10. Inversion of Control with Spring
      1. Inversion of Control and dependency injection
        1. Dependency push versus dependency pull
        2. Dependency injection
      2. Inversion of Control in Spring
        1. Application definition
        2. Implementing non-IoC-style code
        3. Applying IoC
          1. Setter injection
          2. Constructor injection
          3. Method injection
        4. Bean configuration
          1. Singleton versus prototype beans
          2. Wiring beans
            1. The <ref> element
            2. The <idref> element
            3. The <value> element
            4. The <null> element
            5. The <list>, <set>, <map>, and <props> elements
        5. Automatic wiring
        6. Annotation-based container configuration
          1. @Required
          2. @Autowired
          3. @Resource
          4. Classpath scanning for annotated classes
        7. Other format forms for bean definition
        8. BeanFactory and ApplicationContext
      3. Applying IoC to Hibernate resources
      4. PropertyEditors
      5. Summary
    16. 11. Spring AOP
      1. Introduction to AOP
        1. Implementing cross-cutting concerns with OOP
        2. AOP terminology
        3. Implementing cross-cutting concerns with AOP
      2. Using Spring AOP with Spring IoC: An example
        1. Implementing the notification concern with OOP
        2. Implementing notification concern with AOP
      3. Spring's AOP framework
        1. Advice
          1. Around advice
          2. Before advice
          3. After returning advice
          4. Throws advice
        2. Pointcuts
          1. Setter and getter pointcut
          2. Name matched pointcut
          3. Regular expression pointcuts
          4. Static matcher pointcut
          5. Dynamic Matcher Pointcut
          6. Pointcut composition
        3. Advisor
        4. Proxy configuration and creation
          1. Using ProxyFactory
          2. Using ProxyFactoryBean
        5. Assembling the AOP components
      4. Moving to Spring 2.x's AOP
        1. AOP configuration with the AOP schema
        2. Defining aspects
        3. Defining pointcuts
        4. Defining an advice
          1. Before advice
          2. After returning advice
          3. After throwing advice
          4. After advice
          5. Around advice
          6. Advice parameters
      5. Summary
    17. 12. Transaction Management
      1. Transaction essentials
        1. Managing transactions in Java
        2. Local versus global transactions
        3. Transaction demarcation
          1. Programmatic transaction demarcation
          2. Declarative transaction demarcation
        4. Transaction attributes
      2. Transactions in Hibernate applications
        1. Using JTA in Hibernate applications
      3. Spring transaction abstraction
        1. How Spring manages transactions
        2. The choice of transaction manager
          1. Using the Hibernate transaction manager
          2. Using the JTA transaction manager
        3. Spring transaction configuration
          1. Transaction configuration in Spring 1.x
            1. Proxy configuration
            2. Autoproxy creation
            3. TransactionProxyFactoryBean
          2. Transaction configuration in Spring 2.x
      4. Caching
      5. Summary
    18. 13. Integrating Hibernate with Spring
      1. The Data Access Object pattern
      2. Service Facade Pattern
      3. Data tier implementation with Hibernate
      4. Data tier implementation with Spring
      5. Spring exception translation
      6. Refactoring DAO classes to use Spring
      7. Configuring Hibernate in a Spring context
      8. Spring transaction management
        1. Local transactions
        2. Global transactions
      9. Summary
    19. 14. Web Development with Hibernate and Spring
      1. Problem definition
        1. Common configuration for Spring integration
      2. The MVC architectural pattern
      3. Spring MVC web framework
        1. Spring MVC workflow
        2. Front controller
        3. Handler mappings
          1. BeanNameUrlHandlerMapping
          2. ControllerClassNameHandlerMapping
          3. SimpleUrlHandlerMapping
        4. Controllers
        5. AbstractController
          1. AbstractCommandController
          2. SimpleFormController
          3. CancellableFormController
        6. Model and View
        7. View resolvers
          1. InternalResourceViewResolver
          2. BeanNameViewResolver
        8. Render the result through JSP
      4. Summary
    20. 15. Testing
      1. An introduction to unit testing
      2. Unit testing with JUnit
        1. The structure of test classes with JUnit
          1. Setting up the preconditions
          2. Call the method being tested and verify the result
          3. Perform finalization operations
        2. Running JUnit tests
      3. Integration testing data-access layer
        1. Verify that the entity class is persistent
        2. Verify that all entity fields are persistent
        3. Verify that HQL works properly
        4. Verify cascading operation
      4. Testing Inversion of Control
      5. Unit testing using mocks
      6. Automating tests with Ant
      7. Summary
    21. A. Some of Hibernate's Advanced Features
      1. Hibernate's Event/Listener model
      2. Interceptor
      3. Filters
        1. Defining a filter
        2. Applying the filter
        3. Enabling the filter and setting up parameters
      4. More on Hibernate configuration
        1. JDBC properties
        2. Hibernate properties
          1. Cache properties
          2. Transaction properties
      5. Logging configuration in Hibernate