You are previewing Java Hibernate Cookbook.
O'Reilly logo
Java Hibernate Cookbook

Book Description

Over 50 recipes to help you build dynamic and powerful real-time Java Hibernate applications

About This Book

  • Learn to associate JDBC and Hibernate with object persistence

  • Manage association mappings, implement basic annotations and learn caching

  • Get to grips with Hibernate fundamentals from installation to developing a business application with this step-by-step guide

  • Who This Book Is For

    This is book for Java developers who now want to learn Hibernate. Good knowledge and understanding of Java is preferred to allow efficient programming of the core elements and applications; it would be helpful if readers are familiar with the basics of SQL.

    What You Will Learn

  • Set up and install Hibernate on your system and explore different ways in which Hibernate can be configured

  • Learn the basic concepts and fundamentals of Java Hibernate

  • Define mappings without a use of XML file using Annotations

  • Persist collection elements such as list, map, set and array

  • Explore the various mapping options and learn to work with Hibernate associations

  • Understand advanced Hibernate concepts such as caching and inheritance

  • Develop an engaging and robust real-world hibernate application based on a common business scenario

  • Integrate Hibernate with other frameworks to develop robust enterprise applications

  • In Detail

    Hibernate is a database independent technology, so the same code will work for all databases. It helps a Java developer write a query by mapping Java bean to database tables and help create tuned queries that boost performance. Even with limited SQL knowledge one can easily perform database operations. This makes the development faster and more accurate than JDBC. Hibernate supports useful features like connection pooling, caching, and inheritance etc.

    This book will provide a useful hands-on guide to Hibernate to accomplish the development of a real-time Hibernate application.

    We will start with the basics of Hibernate, which include setting up Hibernate – the pre-requisites and multiple ways of configuring Hibernate using Java. We will then dive deep into the fundamentals of Hibernate such as SessionFactory, session, criteria, working with objects and criteria. This will help a developer have a better understanding of how Hibernate works and what needs to be done to run a Hibernate application. Moving on, we will learn how to work with annotations, associations and collections. In the final chapters, we will see explore querying, advanced Hibernate concepts and integration with other frameworks.

    Style and approach

    This book is a practical guide filled with carefully organized step-by-step instructions. All recipes are arranged in an easy-to understand and clear manner allowing you to apply the solutions to other situations.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Java Hibernate Cookbook
      1. Table of Contents
      2. Java Hibernate Cookbook
      3. Credits
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Setting Up Hibernate
        1. Introduction
        2. Getting the required libraries for hibernate
          1. Manually downloading
          2. How to do it…
          3. Using Maven
          4. How to do it…
        3. Creating a hibernate persistent class
          1. Getting ready
          2. How to do it...
          3. There's more…
        4. Providing an XML-based hibernate mapping
          1. How to do it…
          2. How it works…
          3. There's more…
        5. Providing an annotation-based hibernate mapping
          1. How to do it…
          2. How it works…
            1. Declaring a class — Table for the database
            2. Declaring an ID — The primary key for the table
            3. Referencing an object
          3. There's more…
        6. Providing a hibernate configuration using an XML file
          1. How to do it...
          2. How it works...
          3. There's more…
        7. Providing a hibernate configuration using the properties file
          1. How to do it…
          2. How it works…
        8. Configuring hibernate programmatically
          1. How to do it…
          2. How it works…
      9. 2. Understanding the Fundamentals
        1. Introduction
        2. Building a SessionFactory
          1. How to do it…
          2. How it works…
          3. There's more…
        3. Creating a generic SessionFactory provider class
          1. How to do it…
          2. How it works…
        4. Opening a new session
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Opening a stateless session
          1. How to do it…
        6. Saving an object to the database
          1. How to do it…
          2. How it works…
          3. There's more…
        7. Fetching an object from the database
          1. How to do it…
          2. How it works…
          3. There's more…
            1. Scenario 1
              1. Code for scenario 1
              2. Output for scenario 1
              3. Explanation for scenario 1
            2. Scenario 2
              1. Code for scenario 2
              2. Output for scenario 2
              3. Explanation for scenario 2
            3. Scenario 3
              1. Code for scenario 3
              2. Output for scenario 3
              3. Explanation for scenario 3
        8. Removing an object from the database
          1. How to do it…
            1. Code
            2. Output
          2. How it works…
        9. Updating an object
          1. How to do it…
            1. Code
            2. Output
          2. How it works…
          3. There's more...
        10. Creating a criteria
          1. How to do it...
            1. Code
            2. Output
          2. How it works…
        11. Restricting the results using a criteria
          1. How to do it…
            1. Code
            2. Output
          2. How it works…
        12. Pagination using a criteria
          1. How to do it...
            1. Code
            2. Output
          2. How it works…
        13. Sorting the results
          1. How to do it…
            1. Code
            2. Output
          2. How it works…
        14. Transforming a result
          1. How to do it...
            1. Scenario 1: Converting a result to List
            2. Scenario 2: Converting a result to Map
              1. Code
              2. Output
            3. Scenario 3: Converting a result to user-defined Bean
              1. Code
              2. Output
        15. Using basic projection
          1. How to do it...
            1. Scenario 1:
              1. Code
              2. Output
            2. Scenario 2:
              1. Code
              2. Output
      10. 3. Basic Annotations
        1. Introduction
        2. Declaring a class as an entity and creating a table in the database – @Entity and @Table
          1. How to do it…
          2. How it works…
        3. Creating a column in the table – @Column
          1. How to do it…
          2. There's more…
            1. length
            2. nullable
            3. unique
            4. columnDefinition
            5. scale and precision
            6. insertable and updatable
          3. See also…
        4. Creating a primary key and composite primary key column – @Id and @IdClass
          1. How to do it…
        5. Creating an autogenerator column
          1. How to do it…
            1. Default generation strategy
            2. Sequence generator
            3. Table generator
          2. There's more…
            1. Attributes available in the @GeneratedValue annotation
              1. Strategy
              2. Generator
            2. Attributes available in @TableGenerator annotation
              1. name
              2. table
              3. pkColumnName
              4. valueColumnName
              5. pkColumnValue
              6. initialValue
              7. allocationSize
      11. 4. Working with Collections
        1. Introduction
        2. Persisting List
          1. Getting ready
            1. Creating tables
            2. Creating a class
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
            2. Retrieving a record
              1. Code
              2. Output
            3. Updating a record
              1. Code
              2. Output
            4. Deleting a record
              1. Code
              2. Output
          3. How it works…
          4. There's more…
            1. Creating classes
            2. Creating tables
            3. Inserting a record
              1. Code
              2. Output
            4. Retrieving a record
              1. Code
              2. Output
        3. Persisting Set
          1. Getting ready
            1. Creating a class
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
            2. Retrieving a record
              1. Code
              2. Output
            3. Updating a record
              1. Code
              2. Output
            4. Deleting a record
              1. Code
              2. Output
          3. How it works…
        4. Persisting Map
          1. Getting ready
            1. Creating tables
            2. Creating a class
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
            2. Retrieving a record
              1. Code
              2. Output
            3. Updating a record
              1. Code
              2. Output
            4. Deleting a record
              1. Code
              2. Output
          3. How it works…
        5. Persisting Array
          1. Getting ready
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
            2. Retrieving a record
              1. Code
              2. Output
      12. 5. Working with Associations
        1. Introduction
        2. One-to-one mapping using foreign key association
          1. Getting ready
            1. Creating the tables
            2. Creating the classes
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
          3. How it works...
          4. There's more…
            1. Creating the tables
            2. Creating the classes
            3. Inserting a record
              1. Code
              2. Output
            4. Retrieving a record using the parent record
              1. Code
              2. Output
            5. Retrieving a record using the child record
              1. Code
              2. Output
        3. One-to-one mapping using a common join table
          1. Getting ready
            1. Creating the tables
            2. Creating the classes
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
          3. How it works…
        4. One-to-one mapping using a common primary key
          1. Getting ready
            1. Creating the tables
            2. Creating the classes
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
          3. How it works…
        5. One-to-many mapping or many-to-one mapping
          1. Getting ready
            1. Creating the tables
            2. Creating the classes
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
            2. Retrieving a record – many-to-one-mapping
              1. Code
              2. Output
            3. Retrieving a record – one-to-many-mapping
              1. Code
              2. Output
          3. How it works…
        6. Many-to-many mapping
          1. Getting ready
            1. Creating the tables
            2. Creating the classes
          2. How to do it…
            1. Inserting a record
              1. Code
              2. Output
            2. Retrieving a record using Developer with Technology
              1. Code
              2. Output
            3. Retrieving a record using Technology with Developers
              1. Code
              2. Output
          3. How it works…
      13. 6. Querying
        1. Introduction
        2. Working with an alias
          1. How to do it…
          2. How it works…
          3. There's more…
        3. Performing aggregate operations
          1. Getting ready
            1. Creating the tables
            2. Creating the classes
            3. Inserting data in the tables
          2. How to do it…
            1. Sum
              1. Code
              2. Output
            2. Avg
              1. Code
              2. Output
            3. Min
              1. Code
              2. Output
            4. Max
              1. Code
              2. Output
            5. Count
              1. Code
              2. Output
        4. Executing a subquery using a criteria
          1. How to do it…
            1. Code
            2. Output
          2. How it works…
        5. Executing a native SQL query
          1. How to do it…
            1. Scalar queries
              1. Code
              2. Output
            2. Entity queries
              1. Code
              2. Output
        6. Executing a query using HQL
          1. How to do it…
            1. The FROM clause
              1. Code
              2. Output
              3. Code
              4. Output
            2. The SELECT clause
              1. Code
              2. Output
          2. How it works…
          3. There's more…
            1. The WHERE clause
            2. The ORDER BY clause
            3. The GROUP BY clause
        7. Using formula in hibernate
          1. How to do it…
            1. Code
            2. Output
          2. How it works…
          3. There's more…
            1. Code
            2. Output
        8. Working with NamedQuery
          1. How to do it…
            1. name
            2. query
            3. Code
            4. Output
          2. There's more…
      14. 7. Advanced Concepts
        1. Introduction
        2. Working with a first-level cache
          1. Getting ready
            1. Creating the classes
            2. Creating the tables
          2. How to do it…
            1. Code
            2. Output
          3. How it works…
          4. There's more…
            1. evict (Object object)
              1. Code
              2. Output
            2. clear()
              1. Code
              2. Output
        3. Working with a second-level cache
          1. Getting ready
            1. Enabling a second-level cache
            2. Adding a caching strategy using a POJO class
          2. How to do it…
            1. Code
            2. Output
          3. How it works…
        4. Working with a query cache
          1. Getting ready
            1. Enabling a query cache:
          2. How to do it…
          3. How it works…
          4. There's more...
            1. Code
            2. Output
        5. Working with the table per class hierarchy strategy of inheritance
          1. Getting ready
            1. Creating the classes
            2. Creating the tables
            3. Annotations used in Employee.java
            4. Annotations used in ContractualEmployee.java
            5. Annotations used in PermanentEmployee.java
          2. How to do it…
            1. Code
            2. Output
          3. How it works…
        6. Working with the table per subclass strategy of inheritance
          1. Getting ready
            1. Creating the classes
            2. Creating the tables
          2. How to do it…
            1. Code
            2. Output
          3. How it works…
        7. Working with the table per concrete class strategy of inheritance
          1. Getting ready
            1. Creating the classes
            2. Creating the tables
            3. Annotations used in Employee.java
            4. Annotations used in PermanentEmployee.java and ContractualEmployee.java
          2. How to do it…
            1. Code
            2. Output
          3. How it works…
        8. Working with the versioning of objects
          1. Getting ready
            1. Creating the classes
            2. Creating the tables
          2. How to do it…
            1. Code
            2. Output
          3. How it works…
            1. Code
            2. Output
        9. Maintaining the history of an object
          1. Getting ready
            1. Creating the classes
            2. Creating the tables
          2. How to do it…
            1. Code
            2. Output
          3. How it works…
          4. There's more…
            1. Changing the suffix by changing the property in the configuration file
            2. Changing the prefix by changing the property in the configuration file
            3. Renaming the REV column
            4. Renaming the REVTYPE column
        10. Working with an interceptor
          1. Getting ready
            1. Creating the classes
            2. Creating the tables
          2. How to do it…
            1. Code
            2. Output:
          3. How it works…
            1. The working of InterceptorTest.java
            2. The working of CustomInterceptor.java
              1. The working of onSave Method
              2. The working of preFlush Method
              3. The working of postFlush Method
        11. Working with batch processing
          1. Getting ready
            1. Creating the classes
          2. How to do it…
          3. How it works…
      15. 8. Integration with Other Frameworks
        1. Introduction
        2. Integration with Spring
          1. Getting ready
            1. The project dependencies
            2. Creating the tables
            3. Creating a model class
            4. Creating an interface – DAO
            5. The Spring configuration
            6. Reviewing the project structure
          2. How to do it…
            1. The DAO implementations
              1. Code
              2. Output
          3. How it works…
        3. Integration with Struts
          1. Getting ready
            1. The project dependencies
            2. The hibernate configuration
            3. Creating the tables
            4. Creating a model class
            5. Creating an interface – DAO
            6. The Struts configuration
              1. The deployment descriptor
              2. Creating an Action class
              3. The Struts Action mapping
            7. Reviewing the project structure
          2. How to do it…
            1. Implementing a DAO
            2. Creating view – JSP
          3. How it works…
          4. See also…
      16. Index