没有合适的资源?快使用搜索试试~ 我知道了~
首页精通JPA2:打造强大的Java持久化应用
精通JPA2:打造强大的Java持久化应用
需积分: 10 12 下载量 39 浏览量
更新于2024-07-25
收藏 5.54MB PDF 举报
"Pro JPA2 - Mastering the Java Persistence API 是一本专为希望深入理解JPA2技术的读者编写的指南,由Mike Keith和Merrick Schincariol撰写,并由Linda De Michiel(JPA规范负责人)作序。本书旨在帮助开发者创建稳健的数据驱动应用程序,适用于Java EE6兼容的项目。"
JPA(Java Persistence API)是Java平台上的一个标准,用于处理对象关系映射(ORM),使得开发人员能够在Java应用中以面向对象的方式操作数据库。JPA2是其第二个主要版本,引入了许多新功能和改进,以增强性能、灵活性和可扩展性。
在《Pro JPA2》一书中,作者将详细讲解以下关键知识点:
1. **基础概念**:介绍JPA的基本概念,包括实体(Entity)、实体管理器(EntityManager)、实体管理工厂(EntityManagerFactory)以及持久化上下文(Persistence Context)。这些组件构成了JPA的核心,它们负责管理对象的生命周期和数据库交互。
2. **对象关系映射**:详细阐述如何使用注解或XML配置将Java类映射到数据库表,包括属性映射、关系映射(一对一、一对多、多对多)、继承策略(单表、联合表、表分片)等。
3. **查询语言**:JPA提供了JPQL(Java Persistence Query Language),一种类似SQL的面向对象的查询语言,以及 Criteria API,用于动态构建查询。这两者都允许开发者以类型安全的方式执行数据库查询。
4. **事务管理**:讲解如何在JPA中配置和管理事务,包括声明式事务和编程式事务,以及事务隔离级别和回滚策略。
5. **缓存机制**:JPA支持一级缓存(实体管理器内的缓存)和二级缓存(可选的全局缓存),提高数据访问性能。书中会介绍如何配置和使用缓存。
6. **性能优化**:讨论如何通过配置JPA设置、查询优化、批处理操作和懒加载等策略来提升应用性能。
7. **高级特性**:涵盖实体事件监听、转换服务、复合主键、多租户支持、以及JPA与EJB、Spring框架的集成等进阶主题。
8. **最佳实践**:提供实际项目中的经验分享和建议,帮助开发者避免常见陷阱,实现高效和可维护的JPA应用。
9. **案例研究**:通过具体的示例应用,展示如何在实际场景中应用JPA2,帮助读者将理论知识转化为实践技能。
《Pro JPA2》是JPA初学者和有经验的开发者深入理解JPA2的宝贵资源,它将帮助你掌握这个强大的ORM框架,从而更高效地构建数据驱动的Java应用。
■ 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 上传
点击了解资源详情
2021-01-22 上传
ying_78392
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Walmar_PageFactory_Practice:此练习是为想要学习如何在Automation Framework中实现Page_Factory的新手创建的
- cm32181.rar_GIS编程_Unix_Linux_
- Meta4 ClickOnce Launcher-crx插件
- 4MB3_Replication_COVID
- IBOX-开源
- “ maintainVisibleContentPosition”道具对Android react-native的支持-Android开发
- 取消标记:做书签的开源应用程序
- 前端客户端
- centos-installation--configuration.zip_操作系统开发_PDF_
- C.R._Beginner_Lessons:C ++初学者作业
- Python_Programs:与python相关的基本程序
- ps-local-patrick:Patrick Sherman的本地存储库将用于PointSource项目
- 灰色网站后台登录web2.0模板下载
- mcfly:浏览您的shell历史记录。 伟大的斯科特!
- 开发人员职业框架:一个开放框架,用于软件开发人员围绕职业发展的对话
- vending-machine-kata
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功