没有合适的资源?快使用搜索试试~ 我知道了~
首页精通JPA 2:Java持久化API权威指南
精通JPA 2:Java持久化API权威指南
需积分: 9 6 下载量 86 浏览量
更新于2024-07-28
收藏 5.12MB PDF 举报
"Apress 出版的《Pro JPA 2 Mastering the Java Persistence API》是2009年11月发布的英文完美版,由Mike Keith和Merrick Schincariol撰写,并由Java Persistence API (JPA) 规范负责人Linda De Michiel作序。这本书是关于JPA 2.0的权威指南,适用于构建强大、数据驱动的应用程序,完全符合Java EE 6标准。"
《Pro JPA 2: Mastering the Java™ Persistence API》这本书深入探讨了Java持久化领域的新特性,旨在帮助读者掌握JPA 2.0这一强大的持久化标准。自JPA 1.0发布以来,它已经成为主流的持久化技术,而本书第一版陪伴了许多开发者成长。
JPA 2.0带来了许多新特性,如:
1. **额外的对象关系映射(ORM)**:这使得开发者可以更灵活地将数据库表与Java对象进行映射,提高了数据模型的表达能力。
2. **更多的对象建模灵活性**:提供了更丰富的选项来适应不同复杂度的数据模型,增强了模型设计的可扩展性和适应性。
3. **类型安全的查询**:通过使用注解或接口定义查询,可以避免运行时的SQL拼写错误,提高代码的可靠性和可维护性。
4. **全新的Criteria API**:这是一个强类型、面向对象的API,用于构建动态查询,允许在运行时构建复杂的查询表达式,极大地提高了代码的灵活性和重用性。
书中不仅涵盖了这些新特性,还为已经熟悉JPA 1.0的开发者提供了版本提示,指出哪些功能是在哪个版本中新增的,以便于对比和升级。作者还分享了一些实用的技巧和建议,帮助读者在实际项目中更好地应用JPA,解决可能出现的问题,提升开发效率。
对于希望深入了解JPA 2.0并充分利用其功能的Java开发者来说,这本书是一个不可多得的资源,能够帮助他们创建健壮、高效且易于维护的数据驱动应用程序。通过阅读此书,开发者可以掌握JPA的核心概念、最佳实践,以及如何在Java EE 6环境中集成和优化持久化层。
■ 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 上传
2008-08-22 上传
2018-06-08 上传
108 浏览量
2008-08-22 上传
2008-01-22 上传
2010-01-14 上传
alecliu
- 粉丝: 2
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Lanzador-开源
- basic-roguelike:具有基本功能的经典Roguelike。使用ROT.js教程项目的TypeScript版本作为起点
- MyBookManager.zip_教育系统应用_Java_
- TTKMusicplayer:模仿Kugou音乐的TTKMusicPlayer,该音乐播放器使用基于Qt的qmmp核心库在Windows和Linux上使用。
- 2019年10月10日
- IvmukOS-开源
- 带有嵌入式HTTP服务器的,适用于Android和Appium的高效UI布局检查器应用程序是uiautomatorviewer(monitor.bat)的替代产品。-Android开发
- FilesystemTreeHTML
- basic_course_2020-21_-2
- vue node express 商城项目.zip
- ampp.rar_matlab例程_matlab_
- 组合:Mi底漆组合
- QtAutoUpdater:一个Qt库,用于自动检查更新并安装更新
- 黑白简洁html5单页网站模板
- angularLAB
- Blank-Image-Finder:一点点JS来生成小书签,该小书签查找未设置路径的图像
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功