You are previewing Hibernate: A J2EE™ Developer's Guide.
O'Reilly logo
Hibernate: A J2EE™ Developer's Guide

Book Description

Build fast, high-performance Java database applications with Hibernate.

Hibernate makes it far easier to build robust, high-performance database applications with Java. Now there's a practical, hands-on guide to using Hibernate's flexible, fast object/relational persistence and query services. Will Iverson covers every facet of development with Hibernate, from its mapping system toits advanced query mechanisms and transaction support.

Iverson shows you how to build Hibernate solutions that can integrate with Swing, with JSP, and even with EJBs utilizing bean-managed persistence. Using realistic examples, he demonstrates how to work with persistent objects, manage schema, and optimize database application performance. After you've mastered Hibernate's core techniques, Iverson presents best practices, tips, tricks, and style guidance for even more effective development. Coverage includes

  • Case study applications: starting from object/relational mapping files, Java code, and existing schema

  • Writing Hibernate queries using HQL—Hibernate's object-oriented SQL extension

  • Using Hibernate with Java-based Criteria and Example or native SQL

  • The Hibernate mapping file format in detail: a complete reference

  • How Hibernate handles class and database relationships

  • Managing session and database transactions with Hibernate

  • Tracking and optimizing performance with p6spy and IronTrack SQL

  • Automatically generating DDL scripts that create, update, and drop tables

  • Even if your Java database experience is limited to basic JDBC, this book will help you leverage Hibernate's remarkable power. You'll spend far less time writing code to bridge databases with Java applications—so you can get to market faster, with more features.

    © Copyright Pearson Education. All rights reserved.

    Table of Contents

    1. Copyright
      1. Dedication
    2. Acknowledgments
    3. About the Author
    4. Preface
      1. Required Skills
      2. Roadmap
    5. 1. Overview
      1. Why Object/Relational Mapping?
      2. What Is Hibernate?
      3. Comparing JDBC to Hibernate
      4. Hibernate's Mapping System
      5. Other Java/Database Integration Solutions
        1. Enterprise JavaBeans (EJB) 2.X
        2. Enterprise JavaBeans (EJB) 3.0
        3. Java Data Objects (JDO)
        4. Other Commercial O/R Tools
      6. How to Obtain and Install
        1. Hibernate Distribution
        2. Hibernate Extensions Distribution
        3. Configuration
      7. Supported Databases
        1. Introduction to MySQL
    6. 2. Getting Oriented
      1. Application Architecture
      2. Mapping Files
        1. Mapping Files in Depth
      3. Generating Java Source
        1. Generated Persistent Classes
      4. Application Configuration
        1. Generated Database Schema
      5. Web Application
      6. JSP Interface
        1. List Posts
        2. Create Author
        3. List Authors
        4. Edit Author
        5. Create Post
        6. View Post
        7. Edit Post
        8. Delete Post
        9. Delete Author
        10. Next Steps
    7. 3. Starting from Java
      1. Java Object Model
        1. Java Classes
        2. Working with XDoclet
      2. Generated Mapping Files
      3. Generated Schema
      4. Working with Artifacts and Owners
    8. 4. Starting from an Existing Schema
      1. Initial Schema
      2. Using Middlegen
        1. Obtaining Middlegen
        2. Configuring Middlegen
        3. Running Middlegen
      3. Generated Mapping Files
      4. Generated Java
      5. Working with the Database
    9. 5. Mapping Files
      1. Basic Structure
      2. Mapping File Reference
        1. any
          1. Used By
          2. Contents
          3. Required Attributes
            1. id-type
            2. name
          4. Optional Attributes
            1. access (optional, defaults to property)
            2. cascade (optional, defaults to none)
            3. meta-type (optional, defaults to class)
        2. array
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. table (defaults to name value)
            3. schema (defaults to none)
            4. cascade (defaults to none)
            5. inverse (defaults to false)
            6. order-by (defaults to none)
            7. where (defaults to none)
            8. batch-size (defaults to 1)
            9. outer-join (defaults to auto)
            10. check (defaults to none)
            11. element-class
        3. bag
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. batch size (defaults to 1)
            3. cascade (defaults to none)
            4. check (defaults to none)
            5. inverse (defaults to false)
            6. lazy (defaults to false)
            7. order-by (defaults to none)
            8. outer-join (defaults to auto)
            9. table (defaults to name value)
            10. schema (defaults to none)
            11. where (defaults to none)
        4. cache
          1. Contents
          2. Used By
          3. Required Attributes
            1. usage
        5. class
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. batch-size (defaults to 1)
            2. check (defaults to none)
            3. discriminator-value (defaults to fully qualified class name)
            4. dynamic-insert (defaults to false)
            5. dynamic-update (defaults to false)
            6. lazy (defaults to false)
            7. mutable (defaults to true)
            8. optimistic-lock (defaults to version)
            9. persister (defaults to Hibernate internal)
            10. polymorphism (defaults to implicit)
            11. proxy (defaults to none)
            12. schema (defaults to none)
            13. select-before-update (defaults to false)
            14. table (defaults to unqualified name)
            15. where (defaults to none)
        6. collection-id
          1. Used By
          2. Contents
          3. Required Attributes
            1. column
            2. type
          4. Optional Attributes
            1. length (defaults to 255)
        7. column
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. check (defaults to none)
            2. index (defaults to none)
            3. length (defaults to 255)
            4. not-null (defaults to false unless in an id)
            5. sql-type (defaults to value specified by the parent tag)
            6. unique (defaults to false unless in an id)
            7. unique-key (defaults to none)
        8. component
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. class
            3. insert (defaults to true)
            4. update (defaults to true)
        9. composite-element
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
        10. composite-id
          1. Used By
          2. Contents
          3. Optional Attributes
            1. access (defaults to property)
            2. class (optional unless a name and unsaved-value also specified)
            3. name (optional unless a name and unsaved-value also specified)
            4. unsaved-value (defaults to none)
        11. composite-index
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
        12. discriminator
          1. Used By
          2. Contents
          3. Optional Attributes
            1. column (defaults to class)
            2. force (defaults to false)
            3. length (defaults to 255)
            4. not-null (defaults to true)
            5. type (defaults to string)
        13. dynamic-component
          1. Used By
          2. Contents
          3. Required Attributes
            1. name (required)
          4. Optional Attributes
            1. access (defaults to property)
            2. update (defaults to true)
            3. insert (defaults to true)
        14. element
          1. Used By
          2. Contents
          3. Required Attributes
            1. Type
          4. Optional Attributes
            1. column
            2. length
            3. not-null (defaults to false)
            4. unique (defaults to false)
        15. generator
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
        16. hibernate-mapping
          1. Contents
          2. Optional Attributes
            1. auto-import (defaults to true)
            2. default-access (defaults to property)
            3. default-cascade (defaults to none)
            4. package (default to none)
            5. schema (defaults to none)
        17. id
          1. Used By
          2. Contents
          3. Optional Attributes
            1. access (defaults to property)
            2. column (defaults to name attribute value)
            3. length
            4. name
            5. type
            6. unsaved-value (optional, defaults to null)
        18. idbag
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. batch-size (defaults to 1)
            3. cascade (defaults to none)
            4. check (defaults to none)
            5. lazy (defaults to false)
            6. order-by (defaults to none)
            7. outer-join (defaults to auto)
            8. schema (defaults to none)
            9. table (defaults to name value)
            10. where (defaults to none)
        19. import
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
          4. Optional Attributes
            1. rename (defaults to the qualified name)
        20. index
          1. Used By
          2. Contents
          3. Optional Attributes
            1. column
            2. length
            3. type (required for maps)
        21. index-many-to-any
          1. Used By
          2. Contents
          3. Required Attributes
            1. id-type (required)
          4. Optional Attributes
            1. meta-type (defaults to class)
        22. index-many-to-many
          1. Used By
          2. Contents
          3. Required Attributes
            1. class (required)
          4. Optional Attributes
            1. column (required if no column tag)
            2. foreign-key (defaults to none)
        23. joined-subclass
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. check (defaults to none)
            2. dynamic-insert (defaults to false)
            3. dynamic-update (defaults to false)
            4. extends (defaults to parent class/subclass)
            5. lazy (defaults to false)
            6. persister (defaults to Hibernate internal)
            7. proxy (defaults to none)
            8. schema (defaults to none)
            9. table (defaults to unqualified class name)
        24. key
          1. Used By
          2. Contents
          3. Optional Attributes
            1. column (required if no column tag)
            2. foreign-key (defaults to none)
        25. key-many-to-one
          1. Used By
          2. Contents
          3. Required Attributes
            1. name (required)
          4. Optional Attributes
            1. access (defaults to property)
            2. class
            3. column (defaults to the name attribute)
            4. foreign-key (defaults to none)
        26. key-property
          1. Used By
          2. Contents
          3. Required Attributes
            1. name (required)
          4. Optional Attributes
            1. access (defaults to property)
            2. column (defaults to the name attribute)
            3. length
            4. type
        27. list
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. batch-size (defaults to 1)
            3. cascade (defaults to none)
            4. check (defaults to none)
            5. inverse (defaults to false)
            6. lazy (defaults to false)
            7. outer-join (defaults to auto)
            8. schema (defaults to none)
            9. table (defaults to name value)
            10. where (defaults to none)
        28. many-to-any
          1. Used By
          2. Contents
          3. Required Attributes
            1. id-type
          4. Optional Attributes
            1. meta-type (defaults to class)
        29. many-to-many
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
          4. Optional Attributes
            1. column (required if there is no column tag)
            2. foreign-key (defaults to none)
            3. outer-join (defaults to auto)
        30. many-to-one
          1. Used By
          2. Contents
          3. Required Attributes
            1. name (required)
          4. Optional Attributes
            1. access (defaults to property)
            2. cascade (defaults to none)
            3. class
            4. column (defaults to the name attribute)
            5. foreign-key (defaults to none)
            6. insert (defaults to true)
            7. not-null (defaults to false)
            8. property-ref (defaults to none)
            9. outer-join (defaults to auto)
            10. unique (defaults to false)
            11. update (defaults to true)
        31. map
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. batch-size (defaults to 1)
            3. cascade (defaults to none)
            4. check (defaults to none)
            5. inverse (defaults to false)
            6. lazy (defaults to false)
            7. order-by (defaults to none)
            8. outer-join (defaults to auto)
            9. schema (defaults to none)
            10. table (defaults to name value)
            11. sort (defaults to unsorted)
            12. where (defaults to none)
        32. meta
          1. Used By
          2. Contents
          3. Required Attributes
            1. attribute
          4. Optional Attributes
            1. inherit (defaults to true)
        33. meta-value
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
            2. value
        34. nested-composite-element
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
            2. name
          4. Optional Attributes
            1. access (defaults to property)
        35. one-to-many
          1. Used By
          2. Contents
          3. Required Attributes
            1. class
        36. one-to-one
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. cascade (defaults to none)
            3. class
            4. constrained (defaults to false)
            5. foreign-key (defaults to none)
            6. outer-join (defaults to auto)
            7. property-ref (defaults to none)
        37. param
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
        38. parent
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
        39. primitive-array
          1. Used By
          2. Contents
          3. Required Attributes
            1. name (required)
          4. Optional Attributes
            1. access (defaults to property)
            2. batch-size (defaults to 1)
            3. check (defaults to none)
            4. outer-join (defaults to auto)
            5. schema (defaults to none)
            6. table (defaults to name value)
            7. where (defaults to none)
        40. property
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. column (defaults to the name attribute)
            3. formula (defaults to none)
            4. insert (defaults to true)
            5. length (defaults to 255)
            6. not-null (defaults to false)
            7. type
            8. unique (defaults to false)
            9. update (defaults to true)
        41. query
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
        42. return
          1. Used By
          2. Contents
          3. Required Attributes
            1. alias
            2. class
        43. set
          1. Used By
          2. Contents
          3. Required Attributes
            1. name (required)
          4. Optional Attributes
            1. access (defaults to property)
            2. batch-size (defaults to 1)
            3. cascade (defaults to none)
            4. check (defaults to none)
            5. lazy (defaults to false)
            6. order-by (defaults to none)
            7. outer-join (defaults to auto)
            8. schema (defaults to none)
            9. sort (defaults to unsorted)
            10. table (defaults to name value)
            11. where (defaults to none)
        44. sql-query
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
        45. subclass
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. discriminator-value (defaults to unqualified class name)
            2. dynamic-insert (defaults to false)
            3. dynamic-update (defaults to false)
            4. extends (defaults to super-declaration)
            5. lazy (defaults to false)
            6. persister (defaults to Hibernate internal)
            7. proxy (defaults to none)
        46. synchronize
          1. Used By
          2. Contents
          3. Required Attributes
            1. Table
        47. timestamp
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. column (defaults to the value of the name attribute)
            3. unsaved-value (defaults to undefined)
        48. version
          1. Used By
          2. Contents
          3. Required Attributes
            1. name
          4. Optional Attributes
            1. access (defaults to property)
            2. column (defaults to the value of the name attribute)
            3. type (defaults to integer)
            4. unsaved-value (defaults to undefined)
    10. 6. Persistent Objects
      1. Sessions
        1. Setting up the Configuration
        2. Obtaining the Session
        3. Creating Objects
        4. Finding Objects
        5. Refreshing Objects
        6. Deleting Objects
        7. Updating Objects
      2. Objects and Identity
        1. What Is Identity?
        2. Identity within a Session
        3. Generating Identity (Primary Keys)
        4. Built-In Hibernate Generators
        5. Composite Identity
        6. Unsaved Value
      3. Life-Cycle Methods
    11. 7. Relationships
      1. Database Relationships
        1. Many-to-One
        2. One-to-Many
        3. Many-to-Many
        4. One-to-One
      2. Java Collection Relationships
      3. Java Class Relationships
        1. Subclasses
        2. Joined Subclasses
        3. Components
      4. Any-Based Relationships
      5. Bi-directional Relationships
    12. 8. Queries
      1. HQL
        1. Using Hibern8 IDE
      2. HQL Reference
        1. Notation Reference
      3. Select
        1. Selected Properties List
        2. Aggregate
        3. Collection Properties
      4. From
        1. Join Types
          1. Inner Join
          2. Left/Right Joins
          3. Full Joins
          4. Outer as Inverse of Inner
          5. Fetch Joins
      5. Where
        1. Logical Operations
        2. Boolean Operations
        3. Quantified Expression
        4. Parameter Binding
      6. Group By
      7. Having
      8. Order By
      9. Criteria Queries
        1. Method Chaining
        2. Easily Override Lazy Settings
      10. Native SQL Queries
    13. 9. Transactions
      1. Introduction to Transactions
        1. Sessions, Transactions, and Flushing
      2. Optimistic and Pessimistic Locking
        1. Pessimistic Locks
        2. Optimistic Locks
    14. 10. Performance
      1. Finding and Solving Problems
        1. IronTrack SQL
          1. Configuring IronTrack SQL
          2. Using IronTrack SQL
      2. Queries
        1. Lazy Objects
        2. Collections
      3. Inserts
      4. Connection Pooling
      5. Caching
        1. Understanding Caches
        2. Configuring a Cache
        3. Standard Caches
        4. Using a Custom Cache
          1. Configuring the Custom Cache
          2. Custom Cache Provider
          3. Custom Cache Implementation
          4. Cache Test Object
          5. Testing the Cache
    15. 11. Schema Management
      1. Updating an Existing Schema
        1. Schema Updates from within an Application
        2. Command Line Schema Updates
        3. Ant Task Schema Updates
      2. Generating Update and Drop Scripts
        1. Command-Line Script Generation
        2. Ant Task Script Generation
        3. Generating Multiple Scripts
    16. 12. Best Practices, Style Guide, Tips and Tricks
      1. Reducing Code with Inversion of Control
      2. Reducing Session Creation Impact with ThreadLocal
      3. Using Hibernate as an EJB BMP Solution
      4. Integrating with Other Technologies
      5. Applications That Use Hibernate
      6. Strategies for Getting Started
        1. Where to Start?
        2. Start with Many-to-One and One-to-Many
        3. Profile Database Fetching
    17. 13. Future Directions
      1. Hibernate 3.0
      2. EJB 3.0
      3. Here and Now