0Sir_Apress.Beginning.EJB.3.Application.Development.Sep.2006.pdf

This preview shows page 1 out of 510 pages.

Unformatted text preview: CYAN MAGENTA YELLOW BLACK PANTONE 123 CV EMPOWERING PRODUCTIVITY FOR THE JAVA™ DEVELOPER Beginning EJB™ 3 Application Development: From Novice to Professional Raghu R. Kodali forums.apress.com FOR PROFESSIONALS BY PROFESSIONALS ™ Companion eBook Raghu Kodali and Jonathan Wetherbee with Peter Zadrozny See last page for details on $10 eBook version Beginning ™ EJB 3 Application Development From Novice to Professional Building lightweight EJB™ 3–based applications and services from the ground up. THE APRESS JAVA™ ROADMAP Beginning EJB™ 3 Application Development Pro EJB™ 3 API: Java™ Persistence API SOURCE CODE ONLINE ISBN 1-59059-671-4 54499 US $44.99 Kodali, Wetherbee, Zadrozny Beginning Java™ EE 5 Platform Raghu R. Kodali and Jonathan Wetherbee with Peter Zadrozny Foreword by Floyd Marinescu, Author, EJB Design Patterns Shelve in Java Programming User level: Beginner–Intermediate Beginning ™ Join online discussions: Covers Covers this this Java™ ™ EE 5 API Application Development Jonathan Wetherbee When we came up with the idea for this book, the lightweight EJB™ 3 API was still in its early stages—yet it was clear to us that the EJB spec developers had at last settled upon a model that really made sense. Coming from the EJB 2.x world, it was like a breath of fresh air, and reminded us of how it felt to discover Java™ technology after years of programming C and C++. A new persistence framework and the introduction of annotations, generics, container injection, and interceptors provides the basis for a much more nimble development model; one that has gained elegance through simplicity. So you can see what got our buzz going. While poring over the 800-plus pages of the EJB 3 spec, which spans three separate documents, we saw an opportunity to translate this material into an approachable format that reads well and makes liberal use of examples that you can build, execute, and further explore on your own machine. In this book, we introduce the EJB 3 API through a series of digestible chapters, allowing you to become comfortable with the EJB 3 API one topic at a time, without having to swallow the whole enchilada in one humongous gulp. In the spirit of our Apress Wines Online application, which we use for numerous examples throughout the book, we want you to truly savor and appreciate the richness of the EJB 3 API. After introducing the technology, we visit the main components defined in the spec: session beans, entities and the persistence framework, messagedriven beans, and EJB web services. For each component, we discuss when and where to use it, what its strengths and weakness are, and insights into best usage practices. Following this, we explain how to integrate EJB 3 components into a comprehensive Java EE 5 application, and then turn the spotlight on transaction management, performance analysis, migration, deployment, and how to build solid EJB 3 clients. Our job is to transform you from EJB 3 novice to expert, and we want you to enjoy the ride! Companion eBook Available EJB 3 Dear Reader, THE EXPERT’S VOICE ® IN JAVA™ TECHNOLOGY 6 89253 59671 5 9 781590 596715 this print for content only—size & color not accurate spine = 0.968" 512 page count 6714FMcmp3.qxd 8/31/06 7:06 PM Page i Beginning EJB™ 3 Application Development From Novice to Professional Raghu R. Kodali and Jonathan Wetherbee with Peter Zadrozny 6714FMcmp3.qxd 8/31/06 7:06 PM Page ii Beginning EJB™ 3 Application Development: From Novice to Professional Copyright © 2006 by Raghu R. Kodali and Jonathan Wetherbee with Peter Zadrozny All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-671-5 ISBN-10 (pbk): 1-59059-671-4 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc. is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Technical Reviewer: Tom Marrs Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Senior Project Manager: Sofia Marchant Copy Edit Manager: Nicole LeClerc Copy Editor: Damon Larson Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositors: Dina Quan, Gina Rexrode Proofreader: Linda Marousek Indexer: Julie Grady Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit . For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit . The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at in the Source Code/ Download section. You will need to answer questions pertaining to this book in order to successfully download the code. 6714FMcmp3.qxd 8/31/06 7:06 PM Page iii To my wife, Lakshmi, for her support and patience, and our two-year-old son, Yash, who is a big source of inspiration —Raghu R. Kodali To my wife, Laurel, and our young bear cubs, Nicholas, Patrick, and Jacob —Jonathan Wetherbee 6714FMcmp3.qxd 8/31/06 7:06 PM Page iv 6714FMcmp3.qxd 8/31/06 7:06 PM Page v Contents at a Glance Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER ■CHAPTER 1 2 3 4 5 6 7 Introduction to the EJB 3 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 EJB 3 Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Entities and the Java Persistence API . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Advanced Persistence Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 EJB 3 Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 EJB 3 and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Integrating Session Beans, Entities, Message-Driven Beans, and Web Services . . . . . . . . . . . . . . . . . . . . 205 ■CHAPTER 8 Transaction Support in EJB 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 ■CHAPTER 9 EJB 3 Performance and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 ■CHAPTER 10 Migrating EJB 2.x Applications to EJB 3 . . . . . . . . . . . . . . . . . . . . . . . 321 ■CHAPTER 11 EJB 3 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 ■CHAPTER 12 EJB 3 Client Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 ■APPENDIX Performance: EJB 2 vs. EJB 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 v 6714FMcmp3.qxd 8/31/06 7:06 PM Page vi 6714FMcmp3.qxd 8/31/06 7:06 PM Page vii Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii ■CHAPTER 1 Introduction to the EJB 3 Architecture . . . . . . . . . . . . . . . . . . . . . 1 An Introduction to EJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 What Is EJB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 The EJB Component Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The EJB Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Core Features of EJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Progression of the EJB Spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 EJB 3 Simplified Development Model . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Distributed Computing Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Chapter 1: Introduction to the EJB 3 Architecture . . . . . . . . . . . . . . . 11 Chapter 2: EJB 3 Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 3: Entities and the Java Persistence API . . . . . . . . . . . . . . . . 11 Chapter 4: Advanced Persistence Features . . . . . . . . . . . . . . . . . . . . . 11 Chapter 5: EJB 3 Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 6: EJB 3 and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Chapter 7: Integrating Session Beans, Entities, Message-Driven Beans, and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Chapter 8: Transaction Support in EJB 3 . . . . . . . . . . . . . . . . . . . . . . . 12 Chapter 9: EJB 3 Performance and Testing . . . . . . . . . . . . . . . . . . . . . 12 Chapter 10: Migrating EJB 2.x Applications to EJB 3 . . . . . . . . . . . . 12 Chapter 11: EJB 3 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Chapter 12: EJB 3 Client Applications . . . . . . . . . . . . . . . . . . . . . . . . . 13 vii 6714FMcmp3.qxd viii 8/31/06 7:06 PM Page viii ■CONTENTS Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Prerequisites for Installing the GlassFish Application Server . . . . . . 14 Installing the GlassFish Application Server . . . . . . . . . . . . . . . . . . . . . 15 Environment Variables Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Starting and Testing the GlassFish Installation . . . . . . . . . . . . . . . . . . 20 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 ■CHAPTER 2 EJB 3 Session Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Introduction to Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Types of Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 When Do You Use Session Beans? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Stateless Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 The Bean Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 The Business Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Business Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Dependency Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Callback Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Stateful Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 The Bean Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 The Business Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Business Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Callback Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Client View for Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Packaging, Deploying, and Testing the Session Beans . . . . . . . . . . . . . . . . 50 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Compiling the Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Packaging the Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Deploying the Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Compiling the Client Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Running the Client Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6714FMcmp3.qxd 8/31/06 7:06 PM Page ix ■CONTENTS ■CHAPTER 3 Entities and the Java Persistence API . . . . . . . . . . . . . . . . . . . . . 57 An Entity Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 A Simple JavaBean: Customer.java . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 A Simple Entity: Customer.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 An Entity with Defaults Exposed: Customer.java . . . . . . . . . . . . . . . . 61 Coding Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Example: Annotating Instance Variables . . . . . . . . . . . . . . . . . . . . . . . 66 Example: Annotating Property Accessors. . . . . . . . . . . . . . . . . . . . . . . 67 Declaring the Primary Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Simple Primary Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Composite Primary Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Summary of Entity Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The Persistence Archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The persistence.xml File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The EntityManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Persistence Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Acquiring an EntityManager Instance . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Transaction Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 The Entity Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 The Life Cycle of a New Entity Instance . . . . . . . . . . . . . . . . . . . . . . . . 77 O/R Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The @Table Annotation (Revisited) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 The @Column Annotation (Revisited) . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Complex Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Entity Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 @OneToOne . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 @OneToMany and @ManyToOne . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 @ManyToMany. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Lazy vs. Eager Field Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Cascading Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 ix 6714FMcmp3.qxd x 8/31/06 7:06 PM Page x ■CONTENTS JPQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 @NamedQuery and @NamedQueries . . . . . . . . . . . . . . . . . . . . . . . . . 87 Binding Query Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Bulk Update and Delete Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Complex Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Forward Generation vs. Reverse Engineering . . . . . . . . . . . . . . . . . . . . . . . . 90 Forward Generation == Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Reverse Engineering == Adaption . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Which One Is Right for Your Project? . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Example Application: CustomerOrderManager . . . . . . . . . . . . . . . . . . . . . . . 91 Customer.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 CustomerOrder.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Address.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 CustomerOrderManager.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 CustomerOrderClient.java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 persistence.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 ■CHAPTER 4 Advanced Persistence Features . . . ...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture