没有合适的资源?快使用搜索试试~ 我知道了~
首页Pro JPA 2:精通Java持久化API的全面指南
Pro JPA 2:精通Java持久化API的全面指南
5星 · 超过95%的资源 需积分: 10 1 下载量 147 浏览量
更新于2024-07-23
收藏 5.12MB PDF 举报
"Pro JPA 2: Mastering the Java™ Persistence API 是一本由 Mike Keith 和 Merrick Schincariol 联合撰写的权威指南,针对的是Java™ EE 6标准下的Java持久化API(JPA)的第二版。这本书是自Java™ Persistence API 1.0发布以来的里程碑之作,见证了API从早期的稚嫩发展到如今主流的持久化解决方案。对于那些在第一版中初次接触JPA的专业人士来说,这是一本不可或缺的参考资料。
JPA 2.0带来了众多显著增强,包括但不限于扩展的对象关系映射(Object-Relational Mapping)、更灵活的对象模型设计、类型化的查询以及全新的Criteria API。这些新特性使得开发人员能够构建更加健壮和数据驱动的应用程序。对于已经熟悉JPA 1.0的读者,书中还提供了版本对比和提示,帮助他们了解哪些功能在新版本中有所改进或新增,以便有效迁移和升级。
作为"专家之声®"系列的一部分,这本书由Java™技术领域的专家撰写,旨在为专业开发者提供实用且深入的知识。它不仅讲解理论,而且包含了大量的实践技巧,旨在帮助读者将JPA 2.0的优势应用于实际项目中。无论你是JPA新手还是资深用户,这本书都将是你提升技能,掌握最新JPA标准的宝贵资源。"
在这个详细解读中,作者不仅介绍了JPA 2.0的核心变化,还强调了它如何通过新的功能提升应用程序的性能和可维护性。此外,书中可能还会涉及最佳实践,如如何优化查询性能,如何处理复杂的数据模型,以及如何利用新API来实现更高效的数据访问和管理。Pro JPA 2: Mastering the Java™ Persistence API 是一本为JPA开发者提供全面学习和实战指导的实用书籍。
■ CONTENTS
xiv
Shared Primary Key ...........................................................................................................................283
Multiple Mapped Attributes................................................................................................................284
Using EmbeddedId .............................................................................................................................286
Advanced Mapping Elements...................................................................................... 288
Read-Only Mappings..........................................................................................................................288
Optionality..........................................................................................................................................289
Advanced Relationships.............................................................................................. 289
Using Join Tables...............................................................................................................................290
Avoiding Join Tables ..........................................................................................................................291
Compound Join Columns ...................................................................................................................292
Orphan Removal.................................................................................................................................294
Mapping Relationship State ...............................................................................................................295
Multiple Tables............................................................................................................ 297
Inheritance.................................................................................................................. 300
Class Hierarchies ...............................................................................................................................300
Mapped Superclasses...................................................................................................................301
Transient Classes in the Hierarchy ...............................................................................................303
Abstract and Concrete Classes .....................................................................................................303
Inheritance Models ............................................................................................................................304
Single-Table Strategy ...................................................................................................................304
Joined Strategy.............................................................................................................................307
Table-per-Concrete-Class Strategy ..............................................................................................309
Mixed Inheritance ..............................................................................................................................311
Summary..................................................................................................................... 313
■
Chapter 11: Advanced Topics............................................................................315
SQL Queries ................................................................................................................ 315
Native Queries versus JDBC...............................................................................................................316
Defining and Executing SQL Queries..................................................................................................318
SQL Result Set Mapping.....................................................................................................................320
Mapping Foreign Keys ..................................................................................................................321
Multiple Result Mappings .............................................................................................................321
Mapping Column Aliases...............................................................................................................321
Mapping Scalar Result Columns ...................................................................................................322
Mapping Compound Keys .............................................................................................................324
Mapping Inheritance .....................................................................................................................325
Parameter Binding .............................................................................................................................326
■ CONTENTS
xv
Lifecycle Callbacks ..................................................................................................... 326
Lifecycle Events .................................................................................................................................326
PrePersist and PostPersist............................................................................................................326
PreRemove and PostRemove ........................................................................................................327
PreUpdate and PostUpdate ...........................................................................................................327
PostLoad .......................................................................................................................................327
Callback Methods...............................................................................................................................327
Enterprise Contexts.......................................................................................................................329
Entity Listeners ..................................................................................................................................329
Attaching Entity Listeners to Entities ............................................................................................329
Default Entity Listeners.................................................................................................................331
Inheritance and Lifecycle Events .......................................................................................................331
Inheriting Callback Methods .........................................................................................................331
Inheriting Entity Listeners .............................................................................................................332
Lifecycle Event Invocation Order...................................................................................................332
Validation .................................................................................................................... 335
Using Constraints...............................................................................................................................336
Invoking Validation.............................................................................................................................337
Validation Groups...............................................................................................................................338
Creating New Constraints ..................................................................................................................340
Constraint Annotations..................................................................................................................340
Constraint Implementation Classes ..............................................................................................341
Validation in JPA ................................................................................................................................342
Enabling Validation ............................................................................................................................343
Setting Lifecycle Validation Groups ...................................................................................................343
Concurrency................................................................................................................ 344
Entity Operations................................................................................................................................344
Entity Access......................................................................................................................................345
Refreshing Entity State ............................................................................................... 345
Locking .......................................................................................................................348
Optimistic Locking .............................................................................................................................348
Versioning .....................................................................................................................................349
Advanced Optimistic Locking Modes ............................................................................................350
Recovering from Optimistic Failures.............................................................................................355
Pessimistic Locking ...........................................................................................................................358
Pessimistic Locking Modes...........................................................................................................358
Pessimistic Scope.........................................................................................................................360
■ CONTENTS
xvi
Pessimistic Timeouts ....................................................................................................................360
Recovering From Pessimistic Failures..........................................................................................361
Caching ....................................................................................................................... 361
Sorting Through the Layers................................................................................................................361
Shared Cache.....................................................................................................................................363
Static Configuration of the Cache .................................................................................................365
Dynamic Cache Management .......................................................................................................366
Utility Classes.............................................................................................................. 368
PersistenceUtil ...................................................................................................................................368
PersistenceUnitUtil.............................................................................................................................368
Summary..................................................................................................................... 369
■
Chapter 12: XML Mapping Files ........................................................................371
The Metadata Puzzle................................................................................................... 372
The Mapping File ........................................................................................................ 373
Disabling Annotations ........................................................................................................................373
xml-mapping-metadata-complete................................................................................................374
metadata-complete.......................................................................................................................374
Persistence Unit Defaults...................................................................................................................375
schema .........................................................................................................................................376
catalog ..........................................................................................................................................376
delimited-identifiers......................................................................................................................376
access...........................................................................................................................................377
cascade-persist ............................................................................................................................377
entity-listeners..............................................................................................................................378
Mapping File Defaults ........................................................................................................................378
package ........................................................................................................................................379
schema .........................................................................................................................................379
catalog ..........................................................................................................................................380
access...........................................................................................................................................380
Queries and Generators .....................................................................................................................381
sequence-generator......................................................................................................................381
table-generator .............................................................................................................................382
named-query.................................................................................................................................382
named-native-query .....................................................................................................................383
sql-result-set-mapping.................................................................................................................384
Managed Classes and Mappings .......................................................................................................385
■ CONTENTS
xvii
Attributes ......................................................................................................................................385
Tables............................................................................................................................................386
Identifier Mappings .......................................................................................................................387
Simple Mappings ..........................................................................................................................389
Relationship and Collection Mappings..........................................................................................391
Embedded Object Mappings .........................................................................................................398
Inheritance Mappings ...................................................................................................................401
Lifecycle Events ............................................................................................................................404
Entity Listeners .............................................................................................................................404
Summary..................................................................................................................... 406
■
Chapter 13: Packaging and Deployment ...........................................................407
Configuring Persistence Units..................................................................................... 407
Persistence Unit Name.......................................................................................................................408
Transaction Type................................................................................................................................408
Persistence Provider ..........................................................................................................................408
Data Source .......................................................................................................................................409
Mapping Files.....................................................................................................................................410
Managed Classes...............................................................................................................................411
Local Classes ................................................................................................................................411
Classes in Mapping Files ..............................................................................................................412
Explicitly Listed Classes................................................................................................................412
Additional JARs of Managed Classes............................................................................................413
Shared Cache Mode...........................................................................................................................413
Validation Mode .................................................................................................................................414
Adding Vendor Properties ..................................................................................................................414
Building and Deploying ............................................................................................... 415
Deployment Classpath .......................................................................................................................415
Packaging Options .............................................................................................................................416
EJB JAR.........................................................................................................................................416
Web Archive ..................................................................................................................................418
Persistence Archive ......................................................................................................................419
Persistence Unit Scope ......................................................................................................................420
Outside the Server ...................................................................................................... 420
Configuring the Persistence Unit .......................................................................................................421
Transaction Type...........................................................................................................................421
Data Source ..................................................................................................................................421
Providers.......................................................................................................................................422
■ CONTENTS
xviii
Listing the Entities ........................................................................................................................422
Specifying Properties at Runtime ......................................................................................................423
System Classpath ..............................................................................................................................423
Schema Generation..................................................................................................... 423
Unique Constraints.............................................................................................................................424
Null Constraints..................................................................................................................................425
String-Based Columns .......................................................................................................................425
Floating Point Columns ......................................................................................................................426
Defining the Column...........................................................................................................................426
Summary..................................................................................................................... 427
■
Chapter 14: Testing ...........................................................................................429
Testing Enterprise Applications .................................................................................. 429
Terminology .......................................................................................................................................430
Testing Outside the Server.................................................................................................................431
Test Frameworks ...............................................................................................................................432
Unit Testing................................................................................................................. 433
Testing Entities ..................................................................................................................................433
Testing Entities in Components .........................................................................................................434
The Entity Manager in Unit Tests .......................................................................................................436
Integration Testing...................................................................................................... 439
Using the Entity Manager...................................................................................................................439
Test Setup and Teardown .............................................................................................................441
Switching Configurations for Testing............................................................................................442
Minimizing Database Connections................................................................................................444
Components and Persistence ............................................................................................................445
Transaction Management .............................................................................................................445
Container-Managed Entity Managers ...........................................................................................450
Other Services...............................................................................................................................452
Using an Embedded EJB Container for Integration Testing ..........................................................453
Best Practices............................................................................................................. 455
Summary..................................................................................................................... 455
■
Chapter 15: Migration .......................................................................................457
Migrating from CMP Entity Beans............................................................................... 457
Scoping the Challenge .......................................................................................................................458
Entity Bean Conversion ......................................................................................................................459
Converting the Business Interface ................................................................................................459
剩余537页未读,继续阅读
2010-09-18 上传
2009-12-06 上传
2012-06-09 上传
2010-03-04 上传
点击了解资源详情
点击了解资源详情
点击了解资源详情
点击了解资源详情
点击了解资源详情
JianhuaLuo
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功