没有合适的资源?快使用搜索试试~ 我知道了~
首页Apache OpenJPA 2.1 教程:Java持久化API详解
Apache OpenJPA 2.1 教程:Java持久化API详解
需积分: 10 4 下载量 100 浏览量
更新于2024-07-29
收藏 3.2MB PDF 举报
"Apache OpenJPA 2.1 User's Guide"
Apache OpenJPA 是一个开源的、Apache 软件基金会开发的Java持久化规范(Java Persistence API, JPA 2.0)实现,该规范定义了如何在Java应用程序中透明地持久化对象。这个用户指南提供了对JPA标准的概述以及OpenJPA的具体使用技术细节。OpenJPA参考指南包含了关于OpenJPA所有方面的详细文档,帮助用户熟悉其高级特性与定制选项,以便在需要时查找特定的OpenJPA功能。
JPA(Java Persistence API)是Java平台上的一个标准,用于管理关系数据库中的数据。它允许开发者将对象模型直接映射到关系数据库,从而简化了数据访问和业务逻辑的处理。JPA的目标是为Java开发者提供一个统一的接口来处理持久化操作,而无需深入理解底层数据库系统。
JPA的核心概念包括:
1. **Entity**:在JPA中,实体代表数据库中的表或视图。实体类必须使用`@Entity`注解,并且通常具有默认构造函数、final字段限制和身份字段。实体可以有继承结构,并通过`@Inheritance`注解定义不同的继承策略。
2. **Entity Identity**:每个实体都有一个唯一标识,即实体身份。这可以通过主键字段(Identity Fields)来实现,主键可以是自动生成的,也可以由用户指定。JPA支持两种类型的实体标识:单值标识和复合标识。
3. **Lifecycle Callbacks**:生命周期回调是在实体生命周期的特定阶段自动调用的方法,如`@PrePersist`、`@PostLoad`等。这些方法可以用来执行预处理或后处理逻辑。此外,还可以使用`@EntityListeners`定义实体监听器,将回调方法集中管理。
4. **Metadata**:元数据是描述实体及其属性的关键部分,包括实体类、字段和关系的配置信息。OpenJPA支持XML和注解两种形式的元数据,它们定义了实体如何映射到数据库,以及持久化行为的规则。
5. **JPA Architecture**:JPA架构包含一系列的组件,如实体管理器(EntityManager)、实体管理器工厂(EntityManagerFactory)、查询语言(JPQL)等。JPA异常层次结构提供了对各种持久化错误的封装,使得开发者可以更好地处理这些问题。
OpenJPA 2.1 版本作为JPA的实现,提供了许多高级特性和自定义选项,如缓存管理、事务控制、性能优化和扩展性,帮助开发者更高效地管理他们的数据持久化需求。通过深入阅读OpenJPA用户指南和参考文档,开发者能够充分利用OpenJPA的功能,构建出高效、可维护的Java应用。

List of Examples
3.1. Interaction of Interfaces Outside Container .................................................................................................14
3.2. Interaction of Interfaces Inside Container ...................................................................................................15
4.1. Persistent Class .....................................................................................................................................17
4.2. Identity Class .......................................................................................................................................21
5.1. Class Metadata .....................................................................................................................................30
5.2. Complete Metadata ................................................................................................................................60
6.1. persistence.xml .....................................................................................................................................70
6.2. Obtaining an EntityManagerFactory ..........................................................................................................70
7.1. Behavior of Transaction Persistence Context ..............................................................................................73
7.2. Behavior of Extended Persistence Context .................................................................................................73
8.1. Persisting Objects ..................................................................................................................................80
8.2. Updating Objects ..................................................................................................................................80
8.3. Removing Objects .................................................................................................................................81
8.4. Detaching and Merging ..........................................................................................................................81
9.1. Grouping Operations with Transactions .....................................................................................................87
10.1. Query Hints ........................................................................................................................................96
10.2. Named Query using Hints .....................................................................................................................98
10.3. .........................................................................................................................................................98
10.4. Delete by Query ................................................................................................................................ 100
10.5. Update by Query ............................................................................................................................... 100
12.1. Creating a SQL Query ........................................................................................................................ 141
12.2. Retrieving Persistent Objects ............................................................................................................... 142
12.3. SQL Query Parameters ....................................................................................................................... 142
13.1. Mapping Classes ............................................................................................................................... 144
13.2. Defining a Unique Constraint ............................................................................................................... 146
13.3. Identity Mapping ............................................................................................................................... 148
13.4. Generator Mapping ............................................................................................................................ 152
13.5. Single Table Mapping ........................................................................................................................ 154
13.6. Joined Subclass Tables ....................................................................................................................... 156
13.7. Table Per Class Mapping .................................................................................................................... 158
13.8. Inheritance Mapping .......................................................................................................................... 160
13.9. Discriminator Mapping ....................................................................................................................... 164
13.10. Basic Field Mapping ......................................................................................................................... 169
13.11. Secondary Table Field Mapping ......................................................................................................... 174
13.12. Embedded Field Mapping .................................................................................................................. 175
13.13. Mapping Mapped Superclass Field ...................................................................................................... 176
13.14. Direct Relation Field Mapping ........................................................................................................... 178
13.15. Join Table Mapping .......................................................................................................................... 180
13.16. Join Table Map Mapping ................................................................................................................... 182
13.17. Full Entity Mappings ........................................................................................................................ 185
2.1. Code Formatting with the Application Id Tool .......................................................................................... 203
3.1. Standard OpenJPA Log Configuration ..................................................................................................... 229
3.2. Standard OpenJPA Log Configuration + All SQL Statements ...................................................................... 230
3.3. Logging to a File ................................................................................................................................. 230
3.4. Standard Log4J Logging ....................................................................................................................... 230
3.5. JDK Log Properties ............................................................................................................................. 231
3.6. Custom Logging Class ......................................................................................................................... 232
4.1. Properties for the OpenJPA DataSource ................................................................................................... 234
4.2. Properties File for a Third-Party DataSource ............................................................................................ 235
4.3. Managed DataSource Configuration ........................................................................................................ 235
4.4. ......................................................................................................................................................... 236
4.5. Using the EntityManager's Connection .................................................................................................... 237
4.6. Using the EntityManagerFactory's DataSource .......................................................................................... 237
4.7. Specifying a DBDictionary ................................................................................................................... 239
xvi

4.8. Specifying a Transaction Isolation .......................................................................................................... 250
4.9. Specifying the Join Syntax Default ......................................................................................................... 250
4.10. Specifying the Join Syntax at Runtime ................................................................................................... 250
4.11. Specifying Connection Usage Defaults .................................................................................................. 252
4.12. Specifying Connection Usage at Runtime ............................................................................................... 252
4.13. Enable SQL statement batching ............................................................................................................ 253
4.14. Disable SQL statement batching ........................................................................................................... 253
4.15. Plug-in custom statement batching implementation .................................................................................. 253
4.16. Specifying Result Set Defaults ............................................................................................................. 255
4.17. Specifying Result Set Behavior at Runtime ............................................................................................. 255
4.18. Schema Creation ............................................................................................................................... 259
4.19. SQL Scripting ................................................................................................................................... 259
4.20. Table Cleanup ................................................................................................................................... 259
4.21. Schema Drop .................................................................................................................................... 259
4.22. Schema Reflection ............................................................................................................................. 259
4.23. Basic Schema ................................................................................................................................... 261
4.24. Full Schema ..................................................................................................................................... 261
5.1. Using the OpenJPA Enhancer ................................................................................................................ 263
5.2. Using the OpenJPA Agent for Runtime Enhancement ................................................................................ 264
5.3. Passing Options to the OpenJPA Agent ................................................................................................... 265
5.4. JPA Datastore Identity Metadata ............................................................................................................ 267
5.5. Finding an Entity with an Entity Identity Field .......................................................................................... 268
5.6. Id Class for Entity Identity Fields ........................................................................................................... 268
5.7. Embedded Id for Entity Identity Fields .................................................................................................... 269
5.8. Using the Application Identity Tool ........................................................................................................ 270
5.9. Specifying Logical Inverses .................................................................................................................. 271
5.10. Enabling Managed Inverses ................................................................................................................. 272
5.11. Log Inconsistencies ............................................................................................................................ 272
5.12. Using Initial Field Values .................................................................................................................... 272
5.13. Using a Large Result Set Iterator .......................................................................................................... 273
5.14. Marking a Large Result Set Field .......................................................................................................... 274
5.15. Configuring the Proxy Manager ........................................................................................................... 275
5.16. Using Externalization ......................................................................................................................... 277
5.17. Querying Externalization Fields ........................................................................................................... 278
5.18. Using External Values ........................................................................................................................ 278
5.19. Custom Fetch Group Metadata ............................................................................................................. 279
5.20. Load Fetch Group Metadata ................................................................................................................ 280
5.21. Using the FetchPlan ........................................................................................................................... 281
5.22. Adding an Eager Field ........................................................................................................................ 282
5.23. Setting the Default Eager Fetch Mode .................................................................................................... 284
5.24. Setting the Eager Fetch Mode at Runtime ............................................................................................... 284
6.1. Setting a Standard Metadata Factory ....................................................................................................... 286
6.2. Setting a Custom Metadata Factory ......................................................................................................... 286
6.3. ......................................................................................................................................................... 287
6.4. OpenJPA Metadata Extensions .............................................................................................................. 292
7.1. Using the Mapping Tool ....................................................................................................................... 293
7.2. Creating the Relational Schema from Mappings ........................................................................................ 294
7.3. Refreshing entire schema and cleaning out tables ...................................................................................... 295
7.4. Dropping Mappings and Association Schema ........................................................................................... 295
7.5. Create DDL for Current Mappings .......................................................................................................... 295
7.6. Create DDL to Update Database for Current Mappings ............................................................................... 295
7.7. Configuring Runtime Forward Mapping .................................................................................................. 296
7.8. Reflection with the Schema Tool ............................................................................................................ 296
7.9. Using the Reverse Mapping Tool ........................................................................................................... 297
7.10. Customizing Reverse Mapping with Properties ........................................................................................ 300
7.11. Validating Mappings .......................................................................................................................... 300
7.12. Configuring Mapping Defaults ............................................................................................................. 302
7.13. Standard JPA Configuration ................................................................................................................ 303
7.14. Datastore Identity Mapping ................................................................................................................. 305
Apache OpenJPA 2.1 User's Guide
xvii

7.15. Overriding Complex Mappings ............................................................................................................ 308
7.16. One-Sided One-Many Mapping ............................................................................................................ 310
7.17. String Key, Entity Value Map Mapping ................................................................................................. 311
7.18. myaddress.xsd .................................................................................................................................. 314
7.19. Address.java ..................................................................................................................................... 315
7.20. USAAddress.java .............................................................................................................................. 315
7.21. CANAddress.java .............................................................................................................................. 316
7.22. Showing annotated Order entity with XML mapping strategy ..................................................................... 316
7.23. Showing creation of Order entity having shipAddress mapped to XML column ............................................. 316
7.24. Sample JPQL queries for XML column mapping ..................................................................................... 317
7.25. Annotated InputStream and Reader ....................................................................................................... 317
7.26. Custom Logging Orphaned Keys .......................................................................................................... 321
8.1. Configuring Transaction Manager Integration ........................................................................................... 323
9.1. Evict from Data Cache ......................................................................................................................... 326
9.2. Using a JPA Extent .............................................................................................................................. 327
9.3. Setting Default Lock Levels .................................................................................................................. 328
9.4. Setting Runtime Lock Levels ................................................................................................................. 329
9.5. Locking APIs ..................................................................................................................................... 329
9.6. Disabling Locking ............................................................................................................................... 331
9.7. Using Savepoints ................................................................................................................................ 333
9.8. Named Seq Sequence ........................................................................................................................... 336
9.9. System Sequence Configuration ............................................................................................................. 336
10.1. Single-JVM Data Cache ...................................................................................................................... 338
10.2. Data Cache Size ................................................................................................................................ 339
10.3. Data Cache Size ................................................................................................................................ 339
10.4. Data Cache Timeout ........................................................................................................................... 339
10.5. Excluding entities .............................................................................................................................. 340
10.6. Including entities ............................................................................................................................... 340
10.7. Bulk updates and cache eviction ........................................................................................................... 341
10.8. Partitioned Data Cache ....................................................................................................................... 341
10.9. Accessing the Cache .......................................................................................................................... 342
10.10. Using the javax.persistence.Cache interface .......................................................................................... 342
10.11. Accessing the StoreCache .................................................................................................................. 342
10.12. StoreCache Usage ............................................................................................................................ 343
10.13. Automatic Data Cache Eviction .......................................................................................................... 344
10.14. Configuring CacheStatistics ............................................................................................................... 344
10.15. Accessing the QueryResultCache ........................................................................................................ 345
10.16. Query Cache Size ............................................................................................................................ 345
10.17. Disabling the Query Cache ................................................................................................................ 346
10.18. Query Cache Eviction Policy .............................................................................................................. 346
10.19. Evicting Queries .............................................................................................................................. 347
10.20. Pinning, and Unpinning Query Results ................................................................................................. 347
10.21. Disabling and Enabling Query Caching ................................................................................................ 348
10.22. Query Replaces Extent ...................................................................................................................... 349
10.23. Hardcoded Selection Value in JPQL Query ........................................................................................... 350
10.24. Parameterized Selection Value in JPQL Query ...................................................................................... 351
12.1. Configuring Detached State ................................................................................................................. 356
12.2. JMS Remote Commit Provider Configuration ......................................................................................... 358
12.3. TCP Remote Commit Provider Configuration ......................................................................................... 359
12.4. JMS Remote Commit Provider transmitting Persisted Object Ids ................................................................ 359
14.1. Using the <config> Ant Tag ................................................................................................................ 367
14.2. Using the Properties Attribute of the <config> Tag .................................................................................. 368
14.3. Using the PropertiesFile Attribute of the <config> Tag ............................................................................. 368
14.4. Using the <classpath> Ant Tag ............................................................................................................. 368
14.5. Using the <codeformat> Ant Tag .......................................................................................................... 368
14.6. Invoking the Enhancer from Ant ........................................................................................................... 369
14.7. Invoking the Application Identity Tool from Ant ..................................................................................... 369
14.8. Invoking the Mapping Tool from Ant .................................................................................................... 370
14.9. Invoking the Reverse Mapping Tool from Ant ......................................................................................... 370
Apache OpenJPA 2.1 User's Guide
xviii

14.10. Invoking the Schema Tool from Ant .................................................................................................... 371
14.11. Using Commons DBCP with Apache Derby .......................................................................................... 372
2.1. Example properties for Derby ................................................................................................................ 384
2.2. Example properties for Interbase ............................................................................................................ 385
2.3. Example properties for JDataStore .......................................................................................................... 385
2.4. Example properties for IBM DB2 ........................................................................................................... 385
2.5. Example properties for Empress ............................................................................................................. 386
2.6. Example properties for H2 Database Engine ............................................................................................. 386
2.7. Example properties for Hypersonic ......................................................................................................... 387
2.8. Example properties for Firebird .............................................................................................................. 387
2.9. Example properties for Informix Dynamic Server ...................................................................................... 387
2.10. Example properties for Ingres .............................................................................................................. 388
2.11. Example properties for InterSystems Cache ............................................................................................ 388
2.12. Example properties for Microsoft Access ............................................................................................... 388
2.13. Example properties for Microsoft SQL Server ......................................................................................... 389
2.14. Example properties for Microsoft FoxPro ............................................................................................... 390
2.15. Example properties for MySQL ............................................................................................................ 390
2.16. Using MySQL Hints .......................................................................................................................... 390
2.17. Example properties for Oracle .............................................................................................................. 391
2.18. Using Oracle Hints ............................................................................................................................ 391
2.19. Property to disable statement batching for Oracle ..................................................................................... 392
2.20. Property to retain connection over the lifetime of the entity manager ........................................................... 392
2.21. Example properties for Pointbase .......................................................................................................... 393
2.22. Example properties for PostgreSQL ...................................................................................................... 393
2.23. Example properties for IBM solidDB .................................................................................................... 393
2.24. Example properties for Sybase ............................................................................................................. 394
Apache OpenJPA 2.1 User's Guide
xix

Part 1. Introduction
剩余415页未读,继续阅读
122 浏览量
点击了解资源详情
点击了解资源详情
2009-09-17 上传
176 浏览量
105 浏览量
2023-12-29 上传
2021-07-08 上传
210 浏览量

qiangbinxie
- 粉丝: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

最新资源
- noteapp全功能构建指南
- 下载topway通威游戏手柄官方驱动,体验PS2震动效果
- VitaminBWv2.02中文汉化版:PS图像黑白转换插件评测
- 现浇钢筋砼组合墙的设计与施工技术解析
- 开源RIR到DNS转换器-构建个性化DNS区域
- Java程序设计复习与练习题答案全集
- 使用VS2013编译live555最新源码指南
- commons-lang3-3.5-bin.zip:最新版本压缩包可用
- PGIS JavaScript二次开发演示与实现细节解析
- 深入理解二维数组及其编程应用
- 林千城开发IIS一键安装工具2016.06.18版
- 纽曼RV96录音笔专用音频转换软件下载
- 野猫影院采集插件功能解析
- 调试工具DebugViewInstDrv的探索与应用
- 球幕影院创新:旋转观影平台的设计与应用
- 实时可视化开发指南:2d-tracer实现交互式绘图
安全验证
文档复制为VIP权益,开通VIP直接复制
