没有合适的资源?快使用搜索试试~ 我知道了~
首页精通JPA2.0:Java持久化API权威指南
精通JPA2.0:Java持久化API权威指南
5星 · 超过95%的资源 需积分: 10 5 下载量 109 浏览量
更新于2024-07-26
收藏 5.54MB PDF 举报
"JPA2.0 API, Pro JPA 2 Mastering the Java Persistence API"
《Pro JPA 2 Mastering the Java Persistence API》是由Mike Keith和Merrick Schincariol合著的专业指南,旨在帮助开发者深入理解和熟练掌握Java持久化API(JPA)的最新版本——JPA 2.0。这本书是Java EE 6兼容的,是开发数据驱动应用程序的权威参考。
JPA,全称为Java Persistence API,是Java平台上的一个标准,用于管理关系数据库中的对象持久化。它提供了一种面向对象的方式来处理数据库操作,简化了数据存储和检索的复杂性,使得开发人员可以更加专注于业务逻辑而不是数据库交互的细节。
在JPA 2.0中,引入了许多增强功能和改进,包括:
1. **Criteria API**:这是一个更强大、类型安全的查询构建工具,允许开发者通过API来构建动态查询,而无需编写SQL语句。这提高了代码的可读性和可维护性。
2. **Entity Graphs**:实体图允许开发者指定在特定操作(如加载或保存)中应包含哪些关联,从而优化性能,避免了不必要的JOIN操作。
3. **Named Queries**:除了传统的静态命名查询外,JPA 2.0还支持动态命名查询,可以在运行时根据需要定义和执行查询。
4. **Multi-tenant Support**:多租户支持使得在一个应用中为多个客户或部门提供隔离的数据存储成为可能。
5. **Enhanced Association Management**:对关联管理进行了增强,包括更好的一对多和多对一关系的处理,以及更灵活的关联映射选项。
6. **Sequence Generator**:引入了序列生成器,为那些不支持自增主键的数据库提供了生成唯一ID的方式。
7. **Temporal Types**:增加了对日期和时间类型的更好支持,包括历史跟踪和审计功能。
8. ** Joined Subclass Inheritance**:改进了继承策略,使得联合子类继承更有效率。
本书包含了丰富的示例代码,这些示例有助于读者理解JPA 2.0的各种特性和用法,并能在实际项目中快速应用。此外,作者Mike Keith和Merrick Schincariol的权威背景确保了内容的准确性和实用性。
《Pro JPA 2 Mastering the Java Persistence API》是任何希望精通JPA 2.0并构建健壮、数据驱动应用程序的Java开发者的必备参考资料。通过深入学习这本书,开发者将能够充分利用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
Download at WoweBook.Com
■ 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
Download at WoweBook.Com
■ 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
Download at WoweBook.Com
■ 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
Download at WoweBook.Com
■ 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
Download at WoweBook.Com
剩余537页未读,继续阅读
点击了解资源详情
点击了解资源详情
点击了解资源详情
2012-06-09 上传
2010-09-18 上传
2010-03-04 上传
2009-12-06 上传
点击了解资源详情
点击了解资源详情
梦百川
- 粉丝: 15
- 资源: 47
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- MC33886MC33886MC33886
- Linux C/C++ 入门必备
- lm7815电源,稳压电源,lm79158电源,稳压电源,正负15付电源
- 如何对Oracle数据库文件进行恢复与备份
- Flex + LCDS + Java 入门教程
- cisco路由器配置ACL详解
- ActionScript 3.0 Cookbook 中文版
- EJB服务器端组件模型
- Lucene_Heritrix的垂直搜索引擎的研究与应用
- for all 用法小结
- makefile入门
- JAAS简介及实例.
- c++常用算法及数据结构
- c语言读取bmp图像c语言读取bmp图像
- COSTAS环性能分析
- 多目标规划的基本解法
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功