Thông tin tài liệu
www.it-ebooks.info
www.it-ebooks.info
Enterprise JavaBeans 3.1
www.it-ebooks.info
www.it-ebooks.info
SIXTH EDITION
Enterprise JavaBeans 3.1
Andrew Lee Rubinger and Bill Burke
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
www.it-ebooks.info
Enterprise JavaBeans 3.1, Sixth Edition
by Andrew Lee Rubinger and Bill Burke
Copyright © 2010 Andrew Lee Rubinger and William J. Burke, Jr. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly
books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor: Mike Loukides
Production Editor: Teresa Elsey
Copyeditor: Genevieve d’Entremont
Proofreader: Teresa Elsey
Indexer: John Bickelhaupt
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
June 1999:
First Edition.
March 2000: Second Edition.
September 2001: Third Edition.
June 2004: Fourth Edition.
May 2006: Fifth Edition.
September 2010: Sixth Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly
Media, Inc. Enterprise JavaBeans 3.1, Sixth Edition, the image of a wallaby and joey, and related
trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-0-596-15802-6
[M]
1283539129
www.it-ebooks.info
Table of Contents
Preface .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Part I. Why Enterprise JavaBeans?
1. Introduction .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Problem Domain 3
Breaking Up Responsibilities 3
Code Smart, Not Hard 6
The Enterprise JavaBeans™ 3.1 Specification 8
Review 10
2. Component Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Server-Side Component Types 12
Session Beans 12
Message-Driven Beans (MDBs) 15
Entity Beans 16
The Java Persistence Model 17
The Model Isn’t Everything 17
3. Container Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Dependency Injection (DI) 20
Concurrency 21
Instance Pooling/Caching 21
Transactions 23
Security 23
Timers 24
Naming and Object Stores 24
Interoperability 25
Lifecycle Callbacks 25
Interceptors 26
v
www.it-ebooks.info
Platform Integration 27
Bringing It Together 27
4. Developing Your First EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Step 1: Preparation 29
Definitions 29
Naming Conventions 32
Conventions for the Examples 32
Step 2: Coding the EJB 33
The Contract 33
The Bean Implementation Class 34
Out-of-Container Testing 35
Integration Testing 36
Summary 39
Part II. Server-Side Component Models
5. The Stateless Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The XML Deployment Descriptor 45
SessionContext 46
EJBContext 47
The Lifecycle of a Stateless Session Bean 49
The Does Not Exist State 50
The Method-Ready Pool 50
Example: The EncryptionEJB 52
The Contract: Business Interfaces 53
Application Exceptions 54
Bean Implementation Class 55
Accessing Environment Properties (Injection and Lookup) 57
Asynchronous Methods 60
6. The Stateful Session Bean .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The Lifecycle of a Stateful Session Bean 64
The Does Not Exist State 65
The Method-Ready State 65
The Passivated State 66
Example: The FileTransferEJB 68
The Contract: Business Interfaces 69
Exceptions 70
Bean Implementation Class 70
POJO Testing Outside the Container 74
Integration Testing 77
vi | Table of Contents
www.it-ebooks.info
7. The Singleton Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Concurrency 82
Shared Mutable Access 84
Container-Managed Concurrency 86
Bean-Managed Concurrency 87
Lifecycle 87
Explicit Startup 87
Example: The RSSCacheEJB 88
Value Objects 89
The Contract: Business Interfaces 92
Bean Implementation Class 92
8. Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
JMS and Message-Driven Beans 98
JMS as a Resource 98
JMS Is Asynchronous 99
JMS Messaging Models 100
Learning More About JMS 103
JMS-Based Message-Driven Beans 103
@MessageDriven 103
The Lifecycle of a Message-Driven Bean 108
The Does Not Exist State 109
The Method-Ready Pool 109
Connector-Based Message-Driven Beans 111
Message Linking 114
Session Beans Should Not Receive Messages 114
The JMS APIs 115
Example: The StatusUpdateEJBs 118
Part III. EJB and Persistence
9. Persistence: EntityManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Entities Are POJOs 128
Managed Versus Unmanaged Entities 130
Persistence Context 130
Packaging a Persistence Unit 133
The Persistence Unit Class Set 135
Obtaining an EntityManager 136
EntityManagerFactory 137
Obtaining a Persistence Context 138
Interacting with an EntityManager 140
Example: A Persistent Employee Registry 141
Table of Contents | vii
www.it-ebooks.info
A Transactional Abstraction 141
Persisting Entities 142
Finding and Updating Entities 144
Removing Entities 147
refresh() 148
contains() and clear() 148
flush() and FlushModeType 148
Locking 149
unwrap() and getDelegate() 149
10. Mapping Persistent Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
The Programming Model 152
The Employee Entity 152
The Bean Class 152
XML Mapping File 154
Basic Relational Mapping 155
Elementary Schema Mappings 155
Primary Keys 157
@Id 157
Table Generators 158
Sequence Generators 159
Primary-Key Classes and Composite Keys 160
Property Mappings 164
@Transient 164
@Basic and FetchType 164
@Lob 166
@Temporal 166
@Enumerated 167
@Embedded Objects 167
11. Entity Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The Seven Relationship Types 171
One-to-One Unidirectional Relationship 173
One-to-One Bidirectional Relationship 176
One-to-Many Unidirectional Relationship 178
Many-to-One Unidirectional Relationship 181
One-to-Many Bidirectional Relationship 182
Many-to-Many Bidirectional Relationship 184
Many-to-Many Unidirectional Relationship 187
Mapping Collection-Based Relationships 188
Ordered List-Based Relationship 188
Map-Based Relationship 189
Detached Entities and FetchType 190
viii | Table of Contents
www.it-ebooks.info
[...]... (EJB 3.1 Specification, page 29): The Enterprise JavaBeans architecture is a [sic] architecture for the development and deployment of component-based business applications Applications written using the Enterprise JavaBeans architecture are scalable, transactional, and multi-user secure These applications may be written once, and then deployed on any server platform that supports the Enterprise JavaBeans. .. contract for interaction between the application and container In the case of Enterprise JavaBeans (EJB) 3.1, this contract is provided by a document jointly developed by experts under the authority of the Java Community Process (http://jcp.org) Its job is to do all the work you shouldn’t be doing The Enterprise JavaBeans 3.1 Specification Just as interfaces in code abstract the “what” from the “how,”... Why Enterprise JavaBeans? If your path to Enterprise Java is anything like mine, you’ve arrived here hoping to be more productive in application development Perhaps you’ve heard some good or bad things about this thing called “EJB,” and you’d like to explore some more Perhaps some EJB technology is already in place at work, and you’re looking to understand it a bit deeper The fact is that Enterprise JavaBeans. .. example code does not require permission We appreciate, but do not require, attribution An attribution usually includes the title, author, publisher, copyright holder, and ISBN For example: Enterprise JavaBeans 3.1, Sixth Edition, by Andrew Lee Rubinger and Bill Burke (O’Reilly) Copyright 2010 Andrew Lee Rubinger and William J Burke, Jr., 9780596158026.” If you feel your use of code examples falls... xvi | Preface www.it-ebooks.info Part I, Why Enterprise JavaBeans? Multiuser distributed system are inherently complex To ignore the issues they present is to ask for trouble down the line Luckily, you don’t have to do all the work yourself; Chapters 1 through 4 outline the benefits of taking advantage of EJB—a component model for simplified development of enterprise applications Part II, Server-Side... scalable, transactional, and multi-user secure These applications may be written once, and then deployed on any server platform that supports the Enterprise JavaBeans specification More simply rewritten: Enterprise JavaBeans is a standard server-side component model for distributed business applications This means that EJB defines a model for piecing together a full system by integrating modules Each component... themselves will have a release cycle that outlives this book, so you may want to check back to get bug fixes, enhancements, and other updates as time goes on Software and Versions This book covers EJB 3.1 and Java Persistence 2.0 It uses Java language features from the Java SE 6 platform Because the focus of this book is on developing vendorindependent EJB components and solutions, we have stayed away... and Elements The Element The and Elements UDDI 2.0 From Standards to Implementation 355 356 356 361 368 369 370 370 371 372 374 375 377 379 380 21 EJB 3.1 and Web Services 381 Accessing Web Services with JAX-RPC Generating JAX-RPC Artifacts from WSDL Calling a Service from an EJB The Deployment... Dan Allen, and Pete Muir: your work has made the examples portion of this book possible, and I’ve no doubt that we’re on the cusp of a revolution that will bring simplicity back to functional testing of Enterprise Java Let’s keep plugging The JBoss Boston User’s Group gave me a stage and place to geek out with friends Until the next meeting, Jesper Pedersen, Shelly McGowan, Scott Marlow, and John Doyle... manual database connections No more object pools or caches It meant no more speaking like a computer It meant writing the programs I wanted to build, not the mechanics underneath My experience with this enterprise software miraculously made me marketable enough to snag a post-college job in the middle of the dot-com fallout, and during my tenure there we enrolled in a weeklong training session from the . Events 33 3
Custom Injection Annotations 33 3
Exception Handling 33 5
Aborting a Method Invocation 33 6
Catch and Rethrow Exceptions 33 6
Interceptor Lifecycle 33 8
Bean. Contents
www.it-ebooks.info
Cascading 19 1
PERSIST 19 2
MERGE 19 2
REMOVE 1 93
REFRESH 1 93
ALL 1 93
When to Use Cascading 19 4
12 . Entity Inheritance . . . . . .
Ngày đăng: 21/02/2014, 04:20
Xem thêm: Tài liệu Enterprise JavaBeans 3.1 ppt