Table Of ContentEJB 3 Developer Guide
A Practical Guide for developers and architects to the
Enterprise Java Beans Standard
Michael Sikora
BIRMINGHAM - MUMBAI
EJB 3 Developer Guide
Copyright © 2008 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of
the information presented. However, the information contained in this book is sold
without warranty, either express or implied. Neither the author, Packt Publishing,
nor its dealers or distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2008
Production Reference: 1160508
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847195-60-9
www.packtpub.com
Cover Image by Michelle O'Kane (michelle@vodafone.ie)
Credits
Author Project Manager
Michael Sikora Abhijeet Deobhakta
Reviewers Project Coordinator
Meenakshi Verma Brinell Catherine Lewis
Chris Mawata
Lawrence Bean Indexer
Monica Ajmera
Senior Acquisition Editor
Douglas Paterson Proofreader
Angie Butcher
Development Editor
Sarah Cullington Production Coordinator
Shantanu Zagade
Technical Editor
Bhupali Khule Cover Work
Shantanu Zagade
Editorial Team Leader
Mithil Kulkarni
About the Author
Michael Sikora is an enterprise architect with a background in the Unified
Process and Java EE. He has a particular interest in object oriented and database
technology. He has worked for many large UK clients such as ICL Fujitsu, Mercury
Communications, Vodafone, and BUPA. He has used Java since 2000. Before
that he spent a decade designing and developing database and data warehouse
systems. He has experience in Oracle, PL/SQL, and C. In the 1980s he worked for
Shell developing exploration software. He graduated in Mathematics from Essex
University and has Masters degrees from London University and Imperial College.
Michael currently resides in London, England and enjoys mountaineering
and still hopes to complete the seven summits. His website is
http://www.ejbconsultants.com.
I would like to thank Arun Zachariah, Amanda Waite, George
Wilk, and Paul Hudson for advice, feedback, and help on this book.
From the Packt publishing staff, I would like in particular to thank
Douglas Paterson for his interest in this project. I would also like to
thank Sarah Cullington, Brinell Lewis, and Bhupali Khule for the
smooth running of the publishing process. Finally I would like to
thank the technical reviewers for some very helpful suggestions.
About the Reviewers
Meenakshi Verma has been part of the IT industry since 1998. She is experienced
in putting up solutions across multiple industry segments using Java/J2EE
technologies. She is currently based in Toronto, Canada and is working with Sapient,
a leading North American Consultancy organization. Sapient is a global organization
which has offices across North America, Europe and India.
Meenakshi has been helping with technical reviews for books published by Packt
Publishing across varied enterprise solutions. Her earlier work includes JasperReports
for Java Developers, Java EE 5 Development using GlassFish Application Server, Practical
Data Analysis and Reporting with BIRT.
I'd like to thank my husband (Atul Verma) for his encouragement
and support throughout the review of this book and many others,
and my three year old son (Prieyaansh Verma) for giving me the
warmth of his love despite my hectic schedules. I also owe thanks
& gratitude to my Father (Mr Bhopal Singh) and Mother
(Mrs Raj Bala) for laying a strong foundation in me and giving
me their unconditional love and support.
Chris Mawata earned his Masters degree at the University of Western Australia
and his Ph.D at the University of Hawaii. He was a professor at the University of
Tennessee at Chattanooga for fifteen years after which he went into Java consulting
and technical training. He currently trains Java developers and writes course
materials for Learning Tree International. He is also sponsored by a National Science
Foundation grant to be a Java consultant for Phenotype Screening Corporation.
Lawrence Bean fell out of Computer Science and into Music Education in his
sophomore year of college. He graduated with a BA in Music Education from the
University of Maine in 1986 and had a ten year career as a Choral Music Educator
in the Kennebunk, Maine school system, while continuing his technology pursuits
as a hobby. His large non-audition groups won silver at the Events America Choral
Festival and his select group was featured on Good Morning America and in Yankee
Magazine for their annual performances of traditional christmas carols at the highly
acclaimed Kennebunkport Christmas Prelude.
He fell back into Computer Science with the offer of a position as Technology
Coordinator at SU#47 in greater Bath, Maine and completed his MS in Computer
Science in 2006. For the past ten years he has literally built the school system's
technology program, from pulling CAT-5 through the walls and constructing PCs
out of spare parts to writing data analysis modules and a custom SIF-compliant
Student Information System, while continuing his musical pursuits as a hobby.
Throughout he has been highly involved in the Maine Music Educators Association,
American Choral Directors Association of Maine, Association of Computer
Technology Educators in Maine, and the Barbershop Harmony Society, holding
various positions of responsibility and bringing the Maine All-State Auditions into
the 21st century with on-line applications, judging, and results processing.
He has been a strong advocate for free, open-source solutions for public schools and
currently presents workshops on IPCop Firewalls and the Koha ILS.
Larry lives with his very patient wife Betsy in Saco, Maine, USA.
Table of Contents
Preface 1
Chapter 1: Introduction to the EJB 3 Architecture 7
Introduction to the Java EE Architecture 7
The EJB 3 Architecture 9
EJB Container Services 11
The JPA Persistence Engine 12
EJB 3 Compared with Earlier Versions 13
Getting Started 14
Installing GlassFish 14
Testing the Installation 16
Accessing the Administrator Console 17
Shutting Down GlassFish 19
Downloading Example Source Code 19
Summary 19
Chapter 2: Session Beans 21
Introduction 21
Stateless Session Beans 22
Annotations 23
Creating a Session Bean Client 25
Running the Example 26
The Program Directory Structure 28
The Ant Build Script 29
The Application Client Container 31
Building the Application 32
Stateless Session Bean's LifeCycle 34
Stateful Session Beans 36
Stateful Session Bean's LifeCycle 39
Local Interfaces 41
Summary 44
Table of Contents
Chapter 3: Entities 45
Introduction 45
EJB 3 Entities 46
Comparison with EJB 2.x Entity Beans 47
Mapping an Entity to a Database Table 48
Introducing the EntityManager 49
Packaging and Deploying Entities 53
The Program Directory Structure 53
Building the Application 54
Field-Based Annotations 56
Generating Primary Keys 57
Table Strategy 57
Sequence Strategy 58
Identity Strategy 59
Auto Strategy 59
Overriding Metadata Defaults 60
Summary 62
Chapter 4: Object/Relational Mapping 63
O/R Mapping Default Behavior 63
A Banking Example Application 64
Customer Entity 65
Account Entity 68
Address Entity 69
Referee Entity 70
Testing the Application 71
O/R Mapping Overriding Defaults 74
Customer Entity 75
Account Entity 79
Address Entity 80
BankServiceBean 82
O/R Mapping Additional Annotations 84
Referee Class 88
BankServiceBean 88
Composite Primary Keys 89
O/R Inheritance Mapping 91
SINGLE_TABLE Strategy 92
JOINED Strategy 94
Table per Concrete Class Strategy 95
Summary 96
[ ii ]
Table of Contents
Chapter 5: The Java Persistence Query Language 97
Introduction 97
Simple Queries 98
Projection 99
Conditional Expressions 99
Aggregate Functions 101
GROUP BY 102
HAVING 102
Queries with Relationships 103
Joins 103
Inner Joins 103
Outer Joins 104
Fetch Joins 104
Collection Comparison Expressions 105
Constructor Expressions 105
SubQueries 106
Functions 107
CONCAT 107
SUBSTRING 107
TRIM 107
LOWER and UPPER 107
LENGTH 108
LOCATE 108
ABS 108
SQRT 108
MOD 108
SIZE 109
Queries with Parameters 109
Positional Parameters 109
Named Parameters 110
Named Queries 110
Handling Date and Time 112
@Temporal annotation 112
Queries with Date Parameters 113
Datetime Functions 113
Bulk Update and Delete 114
Native SQL 114
Summary 118
[ iii ]