Apache OpenJPA User's Guide
Next
Apache OpenJPA User's Guide
Table of Contents
1. Introduction
1. OpenJPA
1. About This Document
2. Java Persistence API
1. Introduction
1. Intended Audience
2. Lightweight Persistence
2. Why JPA?
3. Java Persistence API Architecture
1. JPA Exceptions
4. Entity
1. Restrictions on Persistent Classes
1.1. Default or No-Arg Constructor
1.2. Final
1.3. Identity Fields
1.4. Version Field
1.5. Inheritance
1.6. Persistent Fields
1.7. Conclusions
2. Entity Identity
2.1. Identity Class
2.1.1. Identity Hierarchies
3. Lifecycle Callbacks
3.1. Callback Methods
3.2. Using Callback Methods
3.3. Using Entity Listeners
3.4. Entity Listeners Hierarchy
4. Conclusions
5. Metadata
1. Class Metadata
1.1. Entity
1.2. Id Class
1.3. Mapped Superclass
1.4. Embeddable
1.5. EntityListeners
1.6. Example
2. Field and Property Metadata
2.1. Transient
2.2. Id
2.3. Generated Value
2.4. Embedded Id
2.5. Version
2.6. Basic
2.6.1. Fetch Type
2.7. Embedded
2.8. Many To One
2.8.1. Cascade Type
2.9. One To Many
2.9.1. Bidirectional Relations
2.10. One To One
2.11. Many To Many
2.12. Order By
2.13. Map Key
2.14. Persistent Field Defaults
3. XML Schema
4. Conclusion
6. Persistence
1. persistence.xml
2. Non-EE Use
7. EntityManagerFactory
1. Obtaining an EntityManagerFactory
2. Obtaining EntityManagers
3. Persistence Context
3.1. Transaction Persistence Context
3.2. Extended Persistence Context
4. Closing the EntityManagerFactory
8. EntityManager
1. Transaction Association
2. Entity Lifecycle Management
3. Lifecycle Examples
4. Entity Identity Management
5. Cache Management
6. Query Factory
7. Closing
9. Transaction
1. Transaction Types
2. The EntityTransaction Interface
10. JPA Query
1. JPQL API
1.1. Query Basics
1.2. Relation Traversal
1.3. Fetch Joins
1.4. JPQL Functions
1.5. Polymorphic Queries
1.6. Query Parameters
1.7. Query Hints
1.7.1. Locking Hints
1.7.2. Result Set Size Hint
1.7.3. Isolation Level Hint
1.7.4. Other Fetchplan Hints
1.7.5. Oracle Query Hints
1.7.6. Named Query Hints
1.8. Ordering
1.9. Aggregates
1.10. Named Queries
1.11. Delete By Query
1.12. Update By Query
2. JPQL Language Reference
2.1. JPQL Statement Types
2.1.1. JPQL Select Statement
2.1.2. JPQL Update and Delete Statements
2.2. JPQL Abstract Schema Types and Query Domains
2.2.1. JPQL Entity Naming
2.2.2. JPQL Schema Example
2.3. JPQL FROM Clause and Navigational Declarations
2.3.1. JPQL FROM Identifiers
2.3.2. JPQL Identification Variables
2.3.3. JPQL Range Declarations
2.3.4. JPQL Path Expressions
2.3.5. JPQL Joins
2.3.5.1. JPQL Inner Joins (Relationship Joins)
2.3.5.2. JPQL Outer Joins
2.3.5.3. JPQL Fetch Joins
2.3.6. JPQL Collection Member Declarations
2.3.7. JPQL Polymorphism
2.4. JPQL WHERE Clause
2.5. JPQL Conditional Expressions
2.5.1. JPQL Literals
2.5.2. JPQL Identification Variables
2.5.3. JPQL Path Expressions
2.5.4. JPQL Input Parameters
2.5.4.1. JPQL Positional Parameters
2.5.4.2. JPQL Named Parameters
2.5.5. JPQL Conditional Expression Composition
2.5.6. JPQL Operators and Operator Precedence
2.5.7. JPQL Between Expressions
2.5.8. JPQL In Expressions
2.5.9. JPQL Like Expressions
2.5.10. JPQL Null Comparison Expressions
2.5.11. JPQL Empty Collection Comparison Expressions
2.5.12. JPQL Collection Member Expressions
2.5.13. JPQL Exists Expressions
2.5.14. JPQL All or Any Expressions
2.5.15. JPQL Subqueries
2.5.16. JPQL Functional Expressions
2.5.16.1. JPQL String Functions
2.5.16.2. JPQL Arithmetic Functions
2.5.16.3. JPQL Datetime Functions
2.6. JPQL GROUP BY, HAVING
2.7. JPQL SELECT Clause
2.7.1. JPQL Result Type of the SELECT Clause
2.7.2. JPQL Constructor Expressions
2.7.3. JPQL Null Values in the Query Result
2.7.4. JPQL Aggregate Functions
2.7.4.1. JPQL Aggregate Examples
2.8. JPQL ORDER BY Clause
2.9. JPQL Bulk Update and Delete
2.10. JPQL Null Values
2.11. JPQL Equality and Comparison Semantics
2.12. JPQL BNF
11. SQL Queries
1. Creating SQL Queries
2. Retrieving Persistent Objects with SQL
12. Mapping Metadata
1. Table
2. Unique Constraints
3. Column
4. Identity Mapping
5. Generators
5.1. Sequence Generator
5.2. TableGenerator
5.3. Example
6. Inheritance
6.1. Single Table
6.1.1. Advantages
6.1.2. Disadvantages
6.2. Joined
6.2.1. Advantages
6.2.2. Disadvantages
6.3. Table Per Class
6.3.1. Advantages
6.3.2. Disadvantages
6.4. Putting it All Together
7. Discriminator
8. Field Mapping
8.1. Basic Mapping
8.1.1. LOBs
8.1.2. Enumerated
8.1.3. Temporal Types
8.1.4. The Updated Mappings
8.2. Secondary Tables
8.3. Embedded Mapping
8.4. Direct Relations
8.5. Join Table
8.6. Bidirectional Mapping
8.7. Map Mapping
9. The Complete Mappings
13. Conclusion
3. Reference Guide
1. Introduction
1. Intended Audience
2. Configuration
1. Introduction
2. Runtime Configuration
3. Command Line Configuration
3.1. Code Formatting
4. Plugin Configuration
5. OpenJPA Properties
5.1. openjpa.AutoClear
5.2. openjpa.AutoDetach
5.3. openjpa.BrokerFactory
5.4. openjpa.BrokerImpl
5.5. openjpa.ClassResolver
5.6. openjpa.Compatibility
5.7. openjpa.ConnectionDriverName
5.8. openjpa.Connection2DriverName
5.9. openjpa.ConnectionFactory
5.10. openjpa.ConnectionFactory2
5.11. openjpa.ConnectionFactoryName
5.12. openjpa.ConnectionFactory2Name
5.13. openjpa.ConnectionFactoryMode
5.14. openjpa.ConnectionFactoryProperties
5.15. openjpa.ConnectionFactory2Properties
5.16. openjpa.ConnectionPassword
5.17. openjpa.Connection2Password
5.18. openjpa.ConnectionProperties
5.19. openjpa.Connection2Properties
5.20. openjpa.ConnectionURL
5.21. openjpa.Connection2URL
5.22. openjpa.ConnectionUserName
5.23. openjpa.Connection2UserName
5.24. openjpa.ConnectionRetainMode
5.25. openjpa.DataCache
5.26. openjpa.DataCacheManager
5.27. openjpa.DataCacheTimeout
5.28. openjpa.DetachState
5.29. openjpa.DynamicDataStructs
5.30. openjpa.FetchBatchSize
5.31. openjpa.FetchGroups
5.32. openjpa.FlushBeforeQueries
5.33. openjpa.IgnoreChanges
5.34. openjpa.Id
5.35. openjpa.InverseManager
5.36. openjpa.LockManager
5.37. openjpa.LockTimeout
5.38. openjpa.Log
5.39. openjpa.ManagedRuntime
5.40. openjpa.Mapping
5.41. openjpa.MaxFetchDepth
5.42. openjpa.MetaDataFactory
5.43. openjpa.Multithreaded
5.44. openjpa.Optimistic
5.45. openjpa.OrphanedKeyAction
5.46. openjpa.NontransactionalRead
5.47. openjpa.NontransactionalWrite
5.48. openjpa.ProxyManager
5.49. openjpa.QueryCache
5.50. openjpa.QueryCompilationCache
5.51. openjpa.ReadLockLevel
5.52. openjpa.RemoteCommitProvider
5.53. openjpa.RestoreState
5.54. openjpa.RetainState
5.55. openjpa.RetryClassRegistration
5.56. openjpa.SavepointManager
5.57. openjpa.Sequence
5.58. openjpa.TransactionMode
5.59. openjpa.WriteLockLevel
6. OpenJPA JDBC Properties
6.1. openjpa.jdbc.ConnectionDecorators
6.2. openjpa.jdbc.DBDictionary
6.3. openjpa.jdbc.DriverDataSource
6.4. openjpa.jdbc.EagerFetchMode
6.5. openjpa.jdbc.FetchDirection
6.6. openjpa.jdbc.JDBCListeners
6.7. openjpa.jdbc.LRSSize
6.8. openjpa.jdbc.MappingDefaults
6.9. openjpa.jdbc.MappingFactory
6.10. openjpa.jdbc.ResultSetType
6.11. openjpa.jdbc.Schema
6.12. openjpa.jdbc.SchemaFactory
6.13. openjpa.jdbc.Schemas
6.14. openjpa.jdbc.SQLFactory
6.15. openjpa.jdbc.SubclassFetchMode
6.16. openjpa.jdbc.SynchronizeMappings
6.17. openjpa.jdbc.TransactionIsolation
6.18. openjpa.jdbc.UpdateManager
3. Logging
1. Logging Channels
2. OpenJPA Logging
3. Disabling Logging
4. Log4J
5. Apache Commons Logging
5.1. JDK 1.4 java.util.logging
6. Custom Log
4. JDBC
1. Using the OpenJPA DataSource
2. Using a Third-Party DataSource
2.1. Managed and XA DataSources
3. Runtime Access to DataSource
4. Database Support
4.1. DBDictionary Properties
4.2. MySQLDictionary Properties
4.3. OracleDictionary Properties
5. Setting the Transaction Isolation
6. Setting the SQL Join Syntax
7. Accessing Multiple Databases
8. Configuring the Use of JDBC Connections
9. Large Result Sets
10. Default Schema
11. Schema Reflection
11.1. Schemas List
11.2. Schema Factory
12. Schema Tool
13. XML Schema Format
5. Persistent Classes
1. Persistent Class List
2. Enhancement
2.1. Enhancing at Build Time
2.2. Enhancing JPA Entities on Deployment
2.3. Enhancing at Runtime
2.4. Omitting the OpenJPA enhancer
3. Object Identity
3.1. Datastore Identity
3.2. Entities as Identity Fields
3.3. Application Identity Tool
3.4. Autoassign / Identity Strategy Caveats
4. Managed Inverses
5. Persistent Fields
5.1. Restoring State
5.2. Typing and Ordering
5.3. Calendar Fields and TimeZones
5.4. Proxies
5.4.1. Smart Proxies
5.4.2. Large Result Set Proxies
5.4.3. Custom Proxies
5.5. Externalization
5.5.1. External Values
6. Fetch Groups
6.1. Custom Fetch Groups
6.2. Custom Fetch Group Configuration
6.3. Per-field Fetch Configuration
6.4. Implementation Notes
7. Eager Fetching
7.1. Configuring Eager Fetching
7.2. Eager Fetching Considerations and Limitations
6. Metadata
1. Metadata Factory
2. Additional JPA Metadata
2.1. Datastore Identity
2.2. Surrogate Version
2.3. Persistent Field Values
2.4. Persistent Collection Fields
2.5. Persistent Map Fields
3. Metadata Extensions
3.1. Class Extensions
3.1.1. Fetch Groups
3.1.2. Data Cache
3.1.3. Detached State
3.2. Field Extensions
3.2.1. Dependent
3.2.2. Load Fetch Group
3.2.3. LRS
3.2.4. Inverse-Logical
3.2.5. Read-Only
3.2.6. Type
3.2.7. Externalizer
3.2.8. Factory
3.2.9. External Values
3.3. Example
7. Mapping
1. Forward Mapping
1.1. Using the Mapping Tool
1.2. Generating DDL SQL
1.3. Runtime Forward Mapping
2. Reverse Mapping
2.1. Customizing Reverse Mapping
3. Meet-in-the-Middle Mapping
4. Mapping Defaults
5. Mapping Factory
6. Non-Standard Joins
7. Additional JPA Mappings
7.1. Datastore Identity Mapping
7.2. Surrogate Version Mapping
7.3. Multi-Column Mappings
7.4. Join Column Attribute Targets
7.5. Embedded Mapping
7.6. Collections
7.6.1. Container Table
7.6.2. Element Join Columns
7.6.3. Order Column
7.7. One-Sided One-Many Mapping
7.8. Maps
7.9. Indexes and Constraints
7.9.1. Indexes
7.9.2. Foreign Keys
7.9.3. Unique Constraints
7.10. XML Column Mapping
8. Mapping Limitations
8.1. Table Per Class
9. Mapping Extensions
9.1. Class Extensions
9.1.1. Subclass Fetch Mode
9.1.2. Strategy
9.1.3. Discriminator Strategy
9.1.4. Version Strategy
9.2. Field Extensions
9.2.1. Eager Fetch Mode
9.2.2. Nonpolymorphic
9.2.3. Class Criteria
9.2.4. Strategy
10. Custom Mappings
10.1. Custom Class Mapping
10.2. Custom Discriminator and Version Strategies
10.3. Custom Field Mapping
10.3.1. Value Handlers
10.3.2. Field Strategies
10.3.3. Configuration
11. Orphaned Keys
8. Deployment
1. Factory Deployment
1.1. Standalone Deployment
1.2. EntityManager Injection
2. Integrating with the Transaction Manager
3. XA Transactions
3.1. Using OpenJPA with XA Transactions
9. Runtime Extensions
1. Architecture
1.1. Broker Finalization
1.2. Broker Customization and Finalization
2. JPA Extensions
2.1. OpenJPAEntityManagerFactory
2.2. OpenJPAEntityManager
2.3. OpenJPAQuery
2.4. Extent
2.5. StoreCache
2.6. QueryResultCache
2.7. FetchPlan
2.8. OpenJPAPersistence
3. Object Locking
3.1. Configuring Default Locking
3.2. Configuring Lock Levels at Runtime
3.3. Object Locking APIs
3.4. Lock Manager
3.5. Rules for Locking Behavior
3.6. Known Issues and Limitations
4. Savepoints
4.1. Using Savepoints
4.2. Configuring Savepoints
5. MethodQL
6. Generators
6.1. Runtime Access
7. Transaction Events
8. Non-Relational Stores
10. Caching
1. Data Cache
1.1. Data Cache Configuration
1.2. Data Cache Usage
1.3. Query Cache
1.4. Cache Extension
1.5. Important Notes
1.6. Known Issues and Limitations
2. Query Compilation Cache
11. Remote and Offline Operation
1. Detach and Attach
1.1. Detach Behavior
1.2. Attach Behavior
1.3. Defining the Detached Object Graph
1.3.1. Detached State Field
2. Remote Event Notification Framework
2.1. Remote Commit Provider Configuration
2.1.1. JMS
2.1.2. TCP
2.1.3. Common Properties
2.2. Customization
12. Third Party Integration
1. Apache Ant
1.1. Common Ant Configuration Options
1.2. Enhancer Ant Task
1.3. Application Identity Tool Ant Task
1.4. Mapping Tool Ant Task
1.5. Reverse Mapping Tool Ant Task
1.6. Schema Tool Ant Task
13. Optimization Guidelines
1. JPA Resources
2. Supported Databases
1. Apache Derby
2. Borland Interbase
2.1. Known issues with Interbase
3. JDataStore
4. IBM DB2
4.1. Known issues with DB2
5. Empress
5.1. Known issues with Empress
6. H2 Database Engine
6.1. Known issues with H2 Database Engine
7. Hypersonic
7.1. Known issues with Hypersonic
8. Firebird
8.1. Known issues with Firebird
9. Informix
9.1. Known issues with Informix
10. InterSystems Cache
10.1. Known issues with InterSystems Cache
11. Microsoft Access
11.1. Known issues with Microsoft Access
12. Microsoft SQL Server
12.1. Known issues with SQL Server
13. Microsoft FoxPro
13.1. Known issues with Microsoft FoxPro
14. MySQL
14.1. Known issues with MySQL
15. Oracle
15.1. Using Query Hints with Oracle
15.2. Known issues with Oracle
16. Pointbase
16.1. Known issues with Pointbase
17. PostgreSQL
17.1. Known issues with PostgreSQL
18. Sybase Adaptive Server
18.1. Known issues with Sybase
List of Tables
2.1.
Persistence Mechanisms
10.1.
Interaction of ReadLockMode hint and LockManager
4.1.
OpenJPA Automatic Flush Behavior
5.1.
Externalizer Options
5.2.
Factory Options
10.1.
Data access methods
10.2.
Pre-defined aliases
13.1.
Optimization Guidelines
2.1.
Supported Databases and JDBC Drivers
List of Examples
3.1.
Interaction of Interfaces Outside Container
3.2.
Interaction of Interfaces Inside Container
4.1.
Persistent Class
4.2.
Identity Class
5.1.
Class Metadata
5.2.
Complete Metadata
6.1.
persistence.xml
6.2.
Obtaining an EntityManagerFactory
7.1.
Behavior of Transaction Persistence Context
7.2.
Behavior of Extended Persistence Context
8.1.
Persisting Objects
8.2.
Updating Objects
8.3.
Removing Objects
8.4.
Detaching and Merging
9.1.
Grouping Operations with Transactions
10.1.
Query Hints
10.2.
Named Query using Hints
10.3.
Delete by Query
10.4.
Update by Query
11.1.
Creating a SQL Query
11.2.
Retrieving Persistent Objects
11.3.
SQL Query Parameters
12.1.
Mapping Classes
12.2.
Defining a Unique Constraint
12.3.
Identity Mapping
12.4.
Generator Mapping
12.5.
Single Table Mapping
12.6.
Joined Subclass Tables
12.7.
Table Per Class Mapping
12.8.
Inheritance Mapping
12.9.
Discriminator Mapping
12.10.
Basic Field Mapping
12.11.
Secondary Table Field Mapping
12.12.
Embedded Field Mapping
12.13.
Mapping Mapped Superclass Field
12.14.
Direct Relation Field Mapping
12.15.
Join Table Mapping
12.16.
Join Table Map Mapping
12.17.
Full Entity Mappings
2.1.
Code Formatting with the Application Id Tool
3.1.
Standard OpenJPA Log Configuration
3.2.
Standard OpenJPA Log Configuration + All SQL Statements
3.3.
Logging to a File
3.4.
Standard Log4J Logging
3.5.
JDK 1.4 Log Properties
3.6.
Custom Logging Class
4.1.
Properties for the OpenJPA DataSource
4.2.
Properties File for a Third-Party DataSource
4.3.
Managed DataSource Configuration
4.4.
Using the EntityManager's Connection
4.5.
Using the EntityManagerFactory's DataSource
4.6.
Specifying a DBDictionary
4.7.
Specifying a Transaction Isolation
4.8.
Specifying the Join Syntax Default
4.9.
Specifying the Join Syntax at Runtime
4.10.
Specifying Connection Usage Defaults
4.11.
Specifying Connection Usage at Runtime
4.12.
Specifying Result Set Defaults
4.13.
Specifying Result Set Behavior at Runtime
4.14.
Schema Creation
4.15.
SQL Scripting
4.16.
Table Cleanup
4.17.
Schema Drop
4.18.
Schema Reflection
4.19.
Basic Schema
4.20.
Full Schema
5.1.
Using the OpenJPA Enhancer
5.2.
Using the OpenJPA Agent for Runtime Enhancement
5.3.
Passing Options to the OpenJPA Agent
5.4.
JPA Datastore Identity Metadata
5.5.
Finding an Entity with an Entity Identity Field
5.6.
Id Class for Entity Identity Fields
5.7.
Using the Application Identity Tool
5.8.
Specifying Logical Inverses
5.9.
Enabling Managed Inverses
5.10.
Log Inconsistencies
5.11.
Using Initial Field Values
5.12.
Using a Large Result Set Iterator
5.13.
Marking a Large Result Set Field
5.14.
Configuring the Proxy Manager
5.15.
Using Externalization
5.16.
Querying Externalization Fields
5.17.
Using External Values
5.18.
Custom Fetch Group Metadata
5.19.
Load Fetch Group Metadata
5.20.
Using the FetchPlan
5.21.
Adding an Eager Field
5.22.
Setting the Default Eager Fetch Mode
5.23.
Setting the Eager Fetch Mode at Runtime
6.1.
Setting a Standard Metadata Factory
6.2.
Setting a Custom Metadata Factory
6.3.
OpenJPA Metadata Extensions
7.1.
Using the Mapping Tool
7.2.
Creating the Relational Schema from Mappings
7.3.
Refreshing entire schema and cleaning out tables
7.4.
Dropping Mappings and Association Schema
7.5.
Create DDL for Current Mappings
7.6.
Create DDL to Update Database for Current Mappings
7.7.
Configuring Runtime Forward Mapping
7.8.
Reflection with the Schema Tool
7.9.
Using the Reverse Mapping Tool
7.10.
Customizing Reverse Mapping with Properties
7.11.
Validating Mappings
7.12.
Configuring Mapping Defaults
7.13.
Standard JPA Configuration
7.14.
Datastore Identity Mapping
7.15.
Overriding Complex Mappings
7.16.
One-Sided One-Many Mapping
7.17.
myaddress.xsd
7.18.
Address.Java
7.19.
USAAddress.java
7.20.
CANAddress.java
7.21.
Showing annotated Order entity with XML mapping strategy
7.22.
Showing creation of Order Entity having shipAddress mapped to XML column
7.23.
Sample EJB Queries for XML Column mapping
7.24.
Custom Logging Orphaned Keys
8.1.
Configuring Transaction Manager Integration
9.1.
Evict from Data Cache
9.2.
Using a JPA Extent
9.3.
Setting Default Lock Levels
9.4.
Setting Runtime Lock Levels
9.5.
Locking APIs
9.6.
Disabling Locking
9.7.
Using Savepoints
9.8.
Named Seq Sequence
9.9.
System Sequence Configuration
10.1.
Single-JVM Data Cache
10.2.
Data Cache Size
10.3.
Data Cache Timeout
10.4.
Accessing the StoreCache
10.5.
StoreCache Usage
10.6.
Automatic Data Cache Eviction
10.7.
Accessing the QueryResultCache
10.8.
Query Cache Size
10.9.
Disabling the Query Cache
10.10.
Evicting Queries
10.11.
Pinning, and Unpinning Query Results
10.12.
Disabling and Enabling Query Caching
10.13.
Query Replaces Extent
11.1.
Configuring Detached State
11.2.
TCP Remote Commit Provider Configuration
12.1.
Using the <config> Ant Tag
12.2.
Using the Properties Attribute of the <config> Tag
12.3.
Using the PropertiesFile Attribute of the <config> Tag
12.4.
Using the <classpath> Ant Tag
12.5.
Using the <codeformat> Ant Tag
12.6.
Invoking the Enhancer from Ant
12.7.
Invoking the Application Identity Tool from Ant
12.8.
Invoking the Mapping Tool from Ant
12.9.
Invoking the Reverse Mapping Tool from Ant
12.10.
Invoking the Schema Tool from Ant
2.1.
Example properties for Derby
2.2.
Example properties for Interbase
2.3.
Example properties for JDataStore
2.4.
Example properties for IBM DB2
2.5.
Example properties for Empress
2.6.
Example properties for H2 Database Engine
2.7.
Example properties for Hypersonic
2.8.
Example properties for Firebird
2.9.
Example properties for Informix Dynamic Server
2.10.
Example properties for InterSystems Cache
2.11.
Example properties for Microsoft Access
2.12.
Example properties for Microsoft SQLServer
2.13.
Example properties for Microsoft FoxPro
2.14.
Example properties for MySQL
2.15.
Example properties for Oracle
2.16.
Using Oracle Hints
2.17.
Example properties for Pointbase
2.18.
Example properties for PostgreSQL
2.19.
Example properties for Sybase