Cover image for Java Data Objects

Book description

Java Data Objects revolutionizes the way Java developers interact with databases and other datastores. JDO allows you to store and retrieve objects in a way that's natural to Java programmers. Instead of working with JDBC or EJB's container-managed persistence, you work directly with your Java objects. You don't have to copy data to and from database tables or issue SELECTs to perform queries: your JDO implementation takes care of persistence behind-the-scenes, and you make queries based on the fields of your Java objects, using normal Java syntax.

The result is software that is truly object-oriented: not code that is partially object-oriented, with a large database-shaped lump on the back end. JDO lets you save plain, ordinary Java objects, and does not force you to use different data models and types for dealing with storage. As a result, your code becomes easier to maintain, easier to re-use, and easier to test. And you're not tied to a specific database vendor: your JDO code is entirely database-independent. You don't even need to know whether the datastore is a relational database, an object database, or just a set of files.

This book, written by the JDO Specification Lead and one of the key contributors to the JDO Specification, is the definitive work on the JDO API. It gives you a thorough introduction to JDO, starting with a simple application that demonstrates many of JDO's capabilities. It shows you how to make classes persistent, how JDO maps persistent classes to the database, how to configure JDO at runtime, how to perform transactions, and how to make queries. More advanced chapters cover optional features such as nontransactional access and optimistic transactions. The book concludes by discussing the use of JDO in web applications and J2EE environments.

Whether you only want to read up on an interesting new technology, or are seriously considering an alternative to JDBC or EJB CMP, you'll find that this book is essential. It provides by far the most authoritative and complete coverage available.

Table of Contents

  1. Java Data Objects
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. Foreword
  5. Preface
    1. Who Should Read This Book?
    2. Organization
    3. Software and Versions
    4. Using Code Examples
    5. Conventions
    6. Comments and Questions
    7. Acknowledgments
  6. 1. An Initial Tour
    1. Defining a Persistent Object Model
      1. The Classes to Persist
      2. Declaring Classes to Be Persistent
    2. Project Build Environment
      1. Jars Needed to Use the JDO Reference Implementation
      2. Project Directory Structure
      3. Enhancing Classes for Persistence
    3. Establish a Datastore Connection and Transaction
      1. Acquiring a PersistenceManager
      2. Creating a FOStore Datastore
    4. Operations on Instances
      1. Making Instances Persistent
      2. Accessing Instances
        1. Iterating an extent
        2. Navigating the object model
        3. Executing a query
      3. Modifying an Instance
      4. Deleting Instances
    5. Summary
  7. 2. An Overview of JDO Interfaces
    1. The javax.jdo Package
      1. JDO Exception Classes
    2. The javax.jdo.spi Package
    3. Optional Features
      1. Identity Options
      2. Optional Collections
      3. Transaction-Related Optional Features
  8. 3. JDO Architectures
    1. Architecture Within Application JVM
      1. Single PersistenceManager
      2. Multiple PersistenceManagers Accessing the Same Datastore
      3. Multiple PersistenceManagers Accessing Different Datastores
      4. Shared Implementation Cache
        1. Shared implementation cache within a single JVM
        2. Shared implementation cache distributed among JVMs
    2. Datastore Access
      1. Direct Access of Filesystem or Local Datastore
      2. Remote Access of a JDO Server
      3. Remote Access of a SQL Datastore
    3. System Architectures with a JDO Application
      1. JDO Rich Client with Local Datastore
      2. JDO Applications in a Web Server
      3. JDO Applications as Web Services
      4. Rich Client Connecting to Application Server with EJB Components
      5. Web Server with EJB Server
      6. EJB Session Beans Using Session Bean Façades
      7. JDO Providing Container-Managed Persistence
  9. 4. Defining Persistent Classes
    1. Kinds of Classes and Instances
      1. Kinds of Classes
      2. Kinds of Instances
    2. Java Classes and Metadata
      1. JDO Metadata
        1. Metadata filenames
        2. jdo, package, and class metadata elements
        3. Vendor extensions
        4. Nesting of metadata elements
      2. Inheritance
      3. The Media Mania Object Model
    3. Fields
      1. Supported Types
        1. Collections
        2. Arrays
      2. Persistence of Fields
        1. Controlling field persistence with metadata
        2. Inherited fields
      3. Complete Metadata for the Media Mania Model
  10. 5. Datastore Mappings
    1. Mapping Approaches
    2. Relational Modeling Constructs
      1. SQL 99
    3. Modeling Constructs in Java and Relational Models
    4. Mapping Classes to Tables
    5. Mapping a Single-Valued Field to a Column
      1. Name-Mapping
      2. Type-Mapping
      3. Indexes
    6. Identity
    7. Inheritance
    8. References
    9. Collections and Relationships
      1. Using a Foreign Key
        1. Isomorphic mapping
        2. Defining a collection
        3. Defining a collection and a reference
        4. Managed relationships
      2. Using a Join Table
      3. One-to-One Relationships
      4. Representing Lists and Maps
  11. 6. Class Enhancement
    1. Enhancement Approaches
      1. Reference Enhancer
      2. Vendor-Specific Enhancement
    2. Binary Compatibility
    3. Enhancement Effects on Your Code
    4. Changes Made by the Enhancer
      1. Metadata
        1. Class metadata
        2. Field metadata
        3. Class registration
      2. Instance-Level Data
      3. Field Mediation
        1. Generated accessors and mutators
        2. Management of field values
  12. 7. Establishing a JDO Runtime Environment
    1. Configuring a PersistenceManagerFactory
      1. Connection Properties
      2. Optional Feature Properties
      3. Flags
      4. Flags Settings in Multiple Interfaces
      5. Determining the Optional Features and Default Flag Settings
      6. Vendor-Specific Properties
      7. Nonconfigurable Properties
    2. Acquiring a PersistenceManager
      1. User Object
      2. Closing a PersistenceManager
      3. Closing a PersistenceManagerFactory
    3. Transactions
      1. Properties of Transactions
      2. Transactions and Locking in the Datastore
        1. Transaction-isolation levels
        2. Locking in the datastore
      3. Types of Transactions in JDO
      4. Acquiring a Transaction
      5. Setting the Transaction Type
      6. Transaction Demarcation
        1. Notification of transaction completion
        2. Commit processing
        3. Rollback processing
      7. Restoring Values on Rollback
      8. Determining Whether a Transaction Is Active
    4. Multiple PersistenceManagers
    5. Multithreading
  13. 8. Instance Management
    1. Persistence of Instances
      1. Explicit Persistence
      2. Persistence-by-Reachability
    2. Extent Access
      1. Accessing an Extent
      2. Extent Iteration
      3. Ignoring the Cache
    3. Accessing and Updating Instances
      1. Explicit Marking of Modified Instances
    4. Deleting Instances
      1. Delete Propagation
  14. 9. The JDO Query Language
    1. Query Components
    2. Creating and Initializing a Query
    3. Changes in the Cache
    4. Query Namespaces
      1. Type Names
      2. Field, Parameter, and Variable Names
      3. Keywords
      4. Literals
    5. Query Execution
      1. Parameter Declarations
      2. Executing a Query
      3. Compiling a Query
    6. The Query Filter
      1. General Characteristics of Expressions
      2. Query Operators
        1. Equality and inequality operators
        2. Comparison operators
        3. Boolean operators
        4. Arithmetic operators
        5. String expressions
      3. References
        1. Cast expression
      4. Collections
        1. Variable declaration
        2. The contains( ) method
    7. Ordering Query Results
    8. Closing a Query
  15. 10. Identity
    1. Overview
      1. JDO Identity Types
      2. Metadata
      3. Identity Class
    2. Datastore Identity
    3. Application Identity
      1. Primary-Key Fields
      2. Persistent Class equals() and hashCode( ) Methods
      3. The Application-Identity Class
      4. A Single-Field Primary Key
      5. A Compound Primary Key
      6. A Compound Primary Key That Contains a Foreign Key
      7. Application Identity in an Inheritance Hierarchy
    4. Nondurable Identity
    5. Identity Methods
      1. Get the Identity Class
      2. Get the Identity of an Instance
      3. Getting an Instance via Its Identity
      4. Changing the Application Identity of an Instance
      5. Get the Current Application Identity of an Instance
      6. The String Representation of Identity
    6. Advanced Topics
      1. Choosing an Identity Type
      2. Using Identity Versus a Query
      3. Identity Across PersistenceManagers
  16. 11. Lifecycle States and Transitions
    1. Lifecycle States
      1. Transient
      2. Persistent-New
      3. Hollow
      4. Persistent-Clean
      5. Persistent-Dirty
      6. Persistent-Deleted
      7. Persistent-New-Deleted
    2. State Interrogation
    3. State Transitions
      1. State Transitions During a Datastore Transaction
      2. State Transitions When a Transaction Completes
      3. States Between Transactions
  17. 12. Field Management
    1. Transactional Fields
    2. null Values
    3. Retrieval of Fields
      1. Default Fetch Group
      2. Retrieving All Fields
      3. The Management of Fields
    4. Serialization
    5. Managing Fields During Lifecycle Events
    6. First- and Second-Class Objects
      1. Specifying a Second-Class Object
      2. Embedding Collection Elements
      3. Persistent Classes as Second-Class Objects
      4. Sharing of Instances
  18. 13. Cache Management
    1. Explicit Management of Instances in the Cache
      1. Refreshing Instances
      2. Evicting Instances
    2. Cloning
    3. Transient-Transactional Instances
      1. Transient-Transactional Lifecycle States
        1. Transient-clean
        2. Transient-dirty
      2. State Interrogation
      3. State Transitions
    4. Making a Persistent Instance Transient
  19. 14. Nontransactional Access
    1. Nontransactional Features
    2. Reading Outside a Transaction
    3. Persistent-Nontransactional State
    4. Retaining Values at Transaction Commit
    5. Restoring Values at Transaction Rollback
      1. Before Image
      2. Restoring Persistent Instances
      3. Restoring Persistent-New Instances
    6. Modifying Persistent Instances Outside a Transaction
      1. Hot Cache Example
  20. 15. Optimistic Transactions
    1. Verification at Commit
      1. Recovery from a Failed Transaction
      2. Setting Optimistic Transaction Behavior
      3. Optimistic Example
    2. Optimistic Transaction State Transitions
    3. Deleting Instances
    4. Making Instances Transactional
    5. Modifying Instances
    6. Commit
    7. Rollback
  21. 16. The Web-Server Environment
    1. Web Servers
      1. Accessing the PersistenceManagerFactory
        1. Looking up the PersistenceManagerFactory in JNDI
        2. Constructing the PersistenceManagerFactory from Properties
      2. Servicing Requests
      3. PersistenceManager per Request
      4. PersistenceManager per Application
      5. PersistenceManager per Transactional Request
      6. PersistenceManager per Session
      7. Transactions
      8. JavaServer Pages
    2. Struts with JDO
  22. 17. J2EE Application Servers
    1. Enterprise JavaBeans Architecture
    2. Stateless Session Beans
      1. Configuring the PersistenceManagerFactory
      2. Stateless Session Beans with Container-Managed Transactions
      3. Stateful Session Beans with Container-Managed Transactions
    3. Bean-Managed Transactions
      1. javax.transaction.UserTransaction
      2. javax.jdo.Transaction
      3. Stateless Session Beans with Bean-Managed Transactions
      4. Stateful Session Beans with Bean-Managed Transactions
    4. Message-Driven Beans
    5. Persistent Entities and JDO
      1. Local Persistent Storage
        1. JDO
        2. JDBC
      2. Remote Persistent Storage
        1. Entity beans
        2. Session beans as façades
        3. JDO or CMP?
  23. A. Lifecycle States and Transitions
  24. B. JDO Metadata DTD
  25. C. JDO Interfaces and Exception Classes
    1. Interfaces
      1. Extent
      2. InstanceCallbacks
      3. JDOHelper
      4. PersistenceManager
      5. PersistenceManagerFactory
      6. Query
      7. Transaction
    2. Exceptions
      1. JDOCanRetryException
      2. JDODataStoreException
      3. JDOException
      4. JDOFatalDataStoreException
      5. JDOFatalException
      6. JDOFatalInternalException
      7. JDOFatalUserException
      8. JDOObjectNotFoundException
      9. JDOOptimisticVerificationException
      10. JDOUnsupportedOptionException
      11. JDOUserException
  26. D. JDO Query Language BNF
    1. Parameter Declaration
    2. Variable Declaration
    3. Import Declaration
    4. Ordering Specification
    5. Type Specification
    6. Names
    7. Literal
    8. Filter Expressions
  27. E. Source Code for Examples
    1. The com.mediamania.appserver package
      1. com.mediamania.appserver.CashierBean
      2. com.mediamania.appserver.JDOPlugIn
      3. com.mediamania.appserver.LookupMovieAction
      4. com.mediamania.appserver.MediaValueObject
      5. com.mediamania.appserver.MovieInfo
      6. com.mediamania.appserver.PurchaseValueObject
      7. com.mediamania.appserver.RentalValueObject
    2. The com.mediamania.content package
      1. com.mediamania.content.ContentQueries
      2. com.mediamania.content.Game
      3. com.mediamania.content.MediaContent
      4. com.mediamania.content.MediaPerson
      5. com.mediamania.content.Movie
      6. com.mediamania.content.Role
      7. com.mediamania.content.Studio.java
    3. The com.mediamania.hotcache package
      1. com.mediamania.hotcache.AbstractCache
      2. com.mediamania.hotcache.AbstractDriver
      3. com.mediamania.hotcache.CacheAccess
      4. com.mediamania.hotcache.MasterCache
      5. com.mediamania.hotcache.MasterDriver
      6. com.mediamania.hotcache.SlaveCache
      7. com.mediamania.hotcache.SlaveDriver
    4. The com.mediamania.store package
      1. com.mediamania.store.Address
      2. com.mediamania.store.Customer
      3. com.mediamania.store.MediaItem
      4. com.mediamania.store.Purchase
      5. com.mediamania.store.Rental
      6. com.mediamania.store.RentalCode
      7. com.mediamania.store.RentalItem
      8. com.mediamania.store.StoreQueries
      9. com.mediamania.store.Transaction
  28. Index
  29. About the Authors
  30. Colophon
  31. Copyright