Beginning Java EE 6 with GlassFish 3, 2nd Edition

THE EXPERT’S VOICE ® IN JAVATM TECHNOLOGY
covers

Final Release
of Java™ EE 6
Platform

Beginning

Java EE 6 Platform
with GlassFish 3




From Novice to Professional
Step by step and easy to follow, this book describes
many of the Java™ EE 6 features, and shows them
in action using practical examples

SEcoNd EdITIoN

SOURCE CODE ONLINE

Antonio Goncalves
Expert member of the JSR-316 (Java™ EE 6),
JSR-317 (JPA 2.0), and JSR-318 (EJB™ 3.1) groups

www.it-ebooks.info

www.it-ebooks.info

Beginning Java™ EE 6
Platform
with GlassFish™ 3
Second Edition

■■■
ANTONIO GONCALVES

www.it-ebooks.info


Beginning Java™ EE 6 Platform with GlassFish™ 3, Second Edition
Copyright © 2010 by Antonio Goncalves
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-4302-2889-9
ISBN-13 (electronic): 978-1-4302-2890-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of
the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject
to proprietary rights.
President and Publisher: Paul Manning
Lead Editors: Steve Anglin, Tom Welsh
Technical Reviewer: Jim Farley
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,

Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Coordinating Editor: Mary Tobin
Copy Editor: Ami Knox
Compositor: Kimberly Burton
Indexer: Potomac Indexing, LLC
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springeronline.com.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.
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.


ii

www.it-ebooks.info

To Eloïse, who fills my heart with love.

www.it-ebooks.info

Contents at a Glance
■Foreword ................................................................................................................. xvii
■About the Author .................................................................................................... xviii
■About the Technical Reviewer .................................................................................. xix
■Acknowledgements ................................................................................................... xx
■Preface ..................................................................................................................... xxi
■Chapter 1: Java EE 6 at a Glance.................................................................................1
■Chapter 2: Java Persistence......................................................................................43
■Chapter 3: Object-Relational Mapping.......................................................................61
■Chapter 4: Managing Persistent Objects .................................................................123
■Chapter 5: Callbacks and Listeners.........................................................................167
■Chapter 6: Enterprise Java Beans ...........................................................................179

■Chapter 7: Session Beans and the Timer Service....................................................201
■Chapter 8: Callbacks and Interceptors ....................................................................237
■Chapter 9: Transactions and Security .....................................................................253
■Chapter 10: JavaServer Faces.................................................................................277
■Chapter 11: Pages and Components........................................................................297
■Chapter 12: Processing and Navigation ..................................................................345
■Chapter 13: Sending Messages ...............................................................................377
■Chapter 14: SOAP Web Services ..............................................................................415
■Chapter 15: RESTful Web Services ..........................................................................451
■Index........................................................................................................................485

iv
www.it-ebooks.info

Contents
■Foreword ................................................................................................................. xvii
■About the Author .................................................................................................... xviii
■About the Technical Reviewer .................................................................................. xix
■Acknowledgements ................................................................................................... xx
■Preface ..................................................................................................................... xxi

■Chapter 1: Java EE 6 at a Glance.................................................................................1
Understanding Java EE ......................................................................................................1
A Bit of History.......................................................................................................................................... 2
Standards ................................................................................................................................................. 4
Architecture.............................................................................................................................................. 4
Java EE 6 Specifications......................................................................................................................... 10

What’s New in Java EE 6? ...............................................................................................12
Lighter .................................................................................................................................................... 13
Easier to Use........................................................................................................................................... 15
Richer ..................................................................................................................................................... 16
More Portable ......................................................................................................................................... 18

The CD-BookStore Application.........................................................................................18
Setting Up Your Environment...........................................................................................20
JDK 1.6 ................................................................................................................................................... 20
Maven 2.................................................................................................................................................. 21
JUnit 4 .................................................................................................................................................... 27
Derby 10.6 .............................................................................................................................................. 31
GlassFish v3.0.1 ..................................................................................................................................... 34


v
www.it-ebooks.info

■ CONTENTS

Summary .........................................................................................................................41
■Chapter 2: Java Persistence..................................................................................43
JPA Specification Overview .............................................................................................44
A Brief History of the Specification......................................................................................................... 44
What’s New in JPA 2.0? ......................................................................................................................... 45
Reference Implementation ..................................................................................................................... 45

Understanding Entities.....................................................................................................46
Object-Relational Mapping ..................................................................................................................... 46
Querying Entities .................................................................................................................................... 48
Callbacks and Listeners ......................................................................................................................... 49

Putting it all Together ......................................................................................................50
Writing the Book Entity ........................................................................................................................... 51

Writing the Main Class............................................................................................................................ 52
Persistence Unit for the Main Class........................................................................................................ 53
Compiling with Maven ............................................................................................................................ 54
Running the Main Class with Derby........................................................................................................ 56
Writing the BookTest Class..................................................................................................................... 57
Persistence Unit for the BookTest Class................................................................................................. 59
Running the BookTest Class with Embedded Derby............................................................................... 60

Summary .........................................................................................................................60
■Chapter 3: Object-Relational Mapping...................................................................61
How to Map an Entity.......................................................................................................61
Configuration by Exception..................................................................................................................... 63

Elementary Mapping........................................................................................................64
Tables ..................................................................................................................................................... 65
Primary Keys .......................................................................................................................................... 68
Attributes ................................................................................................................................................ 72
Access Type............................................................................................................................................ 78

vi

www.it-ebooks.info

■ CONTENTS

Collection of Basic Types........................................................................................................................ 81
Map of Basic Types ................................................................................................................................ 83

Mapping with XML ...........................................................................................................84
Embeddables ...................................................................................................................87
Access Type of an Embeddable Class .................................................................................................... 89

Relationship Mapping ......................................................................................................91
Relationships in Relational Databases ................................................................................................... 92
Entity Relationships ................................................................................................................................ 93
Fetching Relationships ......................................................................................................................... 105
Ordering Relationships ......................................................................................................................... 107

Inheritance Mapping......................................................................................................110
Inheritance Strategies .......................................................................................................................... 110
Type of Classes in the Inheritance Hierarchy ....................................................................................... 118


Summary .......................................................................................................................121
■Chapter 4: Managing Persistent Objects .............................................................123
How to Query an Entity ..................................................................................................123
Entity Manager...............................................................................................................127
Obtaining an Entity Manager ................................................................................................................ 128
Persistence Context.............................................................................................................................. 130
Manipulating Entities............................................................................................................................ 132
Cache API ............................................................................................................................................. 142

JPQL...............................................................................................................................145
Select.................................................................................................................................................... 146
From ..................................................................................................................................................... 148
Where ................................................................................................................................................... 148
Order By................................................................................................................................................ 150
Group By and Having ............................................................................................................................ 150
Bulk Delete ........................................................................................................................................... 150

vii
www.it-ebooks.info


■ CONTENTS

Bulk Update .......................................................................................................................................... 151

Queries...........................................................................................................................151
Dynamic Queries .................................................................................................................................. 154
Named Queries ..................................................................................................................................... 155
Native Queries ...................................................................................................................................... 158
Criteria API (or Object-Oriented Queries).............................................................................................. 158

Concurrency...................................................................................................................161
Versioning............................................................................................................................................. 163
Optimistic Locking................................................................................................................................ 164
Pessimistic Locking.............................................................................................................................. 166

Summary .......................................................................................................................166
■Chapter 5: Callbacks and Listeners .....................................................................167
Entity Life Cycle .............................................................................................................167
Callbacks .......................................................................................................................169
Listeners ........................................................................................................................172
Summary .......................................................................................................................177
■Chapter 6: Enterprise Java Beans .......................................................................179
Understanding EJBs.......................................................................................................179
Types of EJBs ....................................................................................................................................... 181
Anatomy of an EJB ............................................................................................................................... 181
EJB Container ....................................................................................................................................... 183
Embedded Container ............................................................................................................................ 184
Dependency Injection and JNDI............................................................................................................ 185
Callback Methods and Interceptors...................................................................................................... 186
Packaging............................................................................................................................................. 186

EJB Specification Overview ...........................................................................................187
History of the Specification .................................................................................................................. 187
What’s New in EJB 3.1 ......................................................................................................................... 188

viii
www.it-ebooks.info

■ CONTENTS

EJB Lite................................................................................................................................................. 189
Reference Implementation ................................................................................................................... 189

Putting It All Together ....................................................................................................190
Writing the Book Entity ......................................................................................................................... 191
Writing the BookEJB Stateless Session Bean....................................................................................... 191
Persistence Unit for the BookEJB......................................................................................................... 193
Writing the Main Class.......................................................................................................................... 194
Compiling and Packaging with Maven ................................................................................................. 194
Deploying on GlassFish......................................................................................................................... 196
Running the Main Class with the Application Client Container............................................................. 197
Writing the BookEJBTest Class............................................................................................................. 197

Summary .......................................................................................................................199
■Chapter 7: Session Beans and the Timer Service................................................201
Session Beans ...............................................................................................................201
Stateless Beans.................................................................................................................................... 202
Stateful Beans ...................................................................................................................................... 205
Singletons............................................................................................................................................. 207
Session Bean Model ............................................................................................................................. 214
Asynchronous Calls .............................................................................................................................. 226
Embeddable Usage............................................................................................................................... 228

The Timer Service..........................................................................................................230
Calendar-Based Expression.................................................................................................................. 231
Automatic Timer Creation..................................................................................................................... 233
Programmatic Timer Creation .............................................................................................................. 234

Summary .......................................................................................................................235
■Chapter 8: Callbacks and Interceptors ................................................................237
Session Beans Life Cycle...............................................................................................237
Stateless and Singleton........................................................................................................................ 237

ix
www.it-ebooks.info

■ CONTENTS

Stateful . ............................................................................................................................................... 239
Callbacks . ............................................................................................................................................ 240

Interceptors ...................................................................................................................243
Around-Invoke Interceptors. ................................................................................................................ 244
Method Interceptors . ........................................................................................................................... 247
Life-Cycle Interceptor . ......................................................................................................................... 249
Chaining and Excluding Interceptors. .................................................................................................. 250

Summary .......................................................................................................................252
■Chapter 9: Transactions and Security .................................................................253
Transactions ..................................................................................................................253
ACID. .................................................................................................................................................... 254
Local Transactions. .............................................................................................................................. 254
Distributed Transactions and XA . ........................................................................................................ 256

Transaction Support in EJB ...........................................................................................259
Container-Managed Transactions . ...................................................................................................... 259
Bean-Managed Transactions. .............................................................................................................. 266

Security..........................................................................................................................268
Principals and Roles . ........................................................................................................................... 268
Authentication and Authorization . ....................................................................................................... 269

Security Support in EJB .................................................................................................269
Declarative Security . ........................................................................................................................... 270
Programmatic Security. ....................................................................................................................... 273

Summary .......................................................................................................................275
■Chapter 10: JavaServer Faces.............................................................................277
Understanding JSF.........................................................................................................278
FacesServlet and faces-config.xml . .................................................................................................... 279
Pages and Components. ...................................................................................................................... 279
Renderer. ............................................................................................................................................. 280

x
www.it-ebooks.info

■ CONTENTS

Converters and Validators .................................................................................................................... 281
Managed Beans and Navigation........................................................................................................... 281
Ajax Support ......................................................................................................................................... 282

Web Interface Specifications Overview .........................................................................282
A Brief History of Web Interfaces.......................................................................................................... 283
JSP 2.2, EL 2.2, and JSTL 1.2............................................................................................................... 283
JSF 2.0.................................................................................................................................................. 283
What’s New in JSF 2.0.......................................................................................................................... 284
Reference Implementation ................................................................................................................... 284

Putting It All Together ....................................................................................................284
Writing the Book Entity ......................................................................................................................... 286
Writing the BookEJB............................................................................................................................. 286
Writing the BookController Managed Bean .......................................................................................... 287
Writing the newBook.xhtml Page ......................................................................................................... 288
Writing the listBooks.xhtml Page.......................................................................................................... 290
Configuration with web.xml.................................................................................................................. 292
Compiling and Packaging with Maven ................................................................................................. 293
Deploying on GlassFish......................................................................................................................... 294
Running the Example............................................................................................................................ 294

Summary .......................................................................................................................295
■Chapter 11: Pages and Components....................................................................297
Web Pages .....................................................................................................................297
HTML .................................................................................................................................................... 297
XHTML .................................................................................................................................................. 299
CSS ....................................................................................................................................................... 301
DOM...................................................................................................................................................... 303
JavaScript............................................................................................................................................. 304

Java Server Pages .........................................................................................................306
Directive Elements................................................................................................................................ 307

xi
www.it-ebooks.info

■ CONTENTS

Scripting Elements ............................................................................................................................... 308
Action Elements.................................................................................................................................... 309
Putting It All Together........................................................................................................................... 310

Expression Language.....................................................................................................312
JSP Standard Tag Library ..............................................................................................314
Core Actions ......................................................................................................................................... 314
Formatting Actions ............................................................................................................................... 316
SQL Actions .......................................................................................................................................... 317
XML Actions.......................................................................................................................................... 319
Functions .............................................................................................................................................. 321

Facelets .........................................................................................................................322
JavaServer Faces ..........................................................................................................323
Life Cycle .............................................................................................................................................. 324
Standard HTML Components ................................................................................................................ 326
Resource Management......................................................................................................................... 336
Composite Components........................................................................................................................ 337
Implicit Objects..................................................................................................................................... 342

Summary .......................................................................................................................343
■Chapter 12: Processing and Navigation ..............................................................345
The MVC Pattern ............................................................................................................345
FacesServlet......................................................................................................................................... 346
FacesContext ........................................................................................................................................ 349
Faces Config......................................................................................................................................... 350

Managed Beans .............................................................................................................351
How to Write a Managed Bean ............................................................................................................. 351
Managed Bean Model........................................................................................................................... 352
Navigation............................................................................................................................................. 356
Message Handling ................................................................................................................................ 361

xii
www.it-ebooks.info

■ CONTENTS

Conversion and Validation .............................................................................................363
Converters ............................................................................................................................................ 363
Custom Converters ............................................................................................................................... 365
Validators.............................................................................................................................................. 366
Custom Validators................................................................................................................................. 367

Ajax ................................................................................................................................368
General Concepts ................................................................................................................................. 368
Support in JSF ...................................................................................................................................... 370
Putting It All Together........................................................................................................................... 372

Summary .......................................................................................................................376
■Chapter 13: Sending Messages ...........................................................................377
Understanding Messages ..............................................................................................377
JMS ...................................................................................................................................................... 378
MDB ...................................................................................................................................................... 378

Messaging Specification Overview................................................................................379
A Brief History of Messaging ................................................................................................................ 379
JMS 1.1 ................................................................................................................................................ 379
EJB 3.1 ................................................................................................................................................. 379
Reference Implementation ................................................................................................................... 380

How to Send and Receive a Message............................................................................380
Java Messaging Service ................................................................................................382
Point-to-Point ....................................................................................................................................... 383
Publish-Subscribe ................................................................................................................................ 384
JMS API ................................................................................................................................................ 385
Selectors............................................................................................................................................... 397
Reliability Mechanisms......................................................................................................................... 397

Message-Driven Beans..................................................................................................400
How to Write an MDB ........................................................................................................................... 400

xiii
www.it-ebooks.info

■ CONTENTS

MDB Model ........................................................................................................................................... 401
MDB as a Consumer ............................................................................................................................. 405
MDB as a Producer............................................................................................................................... 406
Transaction........................................................................................................................................... 407
Handling Exceptions ............................................................................................................................. 408

Putting It All Together ....................................................................................................409
Writing the OrderDTO............................................................................................................................ 409
Writing the OrderSender....................................................................................................................... 409
Writing the OrderMDB........................................................................................................................... 410
Compiling and Packaging with Maven ................................................................................................. 411
Creating the Administered Objects....................................................................................................... 413
Deploying the MDB on GlassFish.......................................................................................................... 413
Running the Example............................................................................................................................ 414

Summary .......................................................................................................................414
■Chapter 14: SOAP Web Services ..........................................................................415
Understanding SOAP Web Services ...............................................................................415
XML....................................................................................................................................................... 416
WSDL .................................................................................................................................................... 417
SOAP..................................................................................................................................................... 417
UDDI...................................................................................................................................................... 417
Transport Protocol ................................................................................................................................ 418

SOAP Web Services Specification Overview..................................................................418
A Brief History of SOAP Web Services .................................................................................................. 418
Java EE Specifications ......................................................................................................................... 418
Reference Implementation ................................................................................................................... 420

How to Invoke a SOAP Web Service...............................................................................420
Java Architecture for XML Binding ................................................................................422
Binding ................................................................................................................................................. 423

xiv
www.it-ebooks.info

■ CONTENTS

Annotations........................................................................................................................................... 425

The Invisible Part of the Iceberg ....................................................................................428
WSDL .................................................................................................................................................... 428
SOAP..................................................................................................................................................... 431

Java API for XML-Based Web Services..........................................................................432
JAX-WS Model...................................................................................................................................... 432
Invoking a SOAP Web Service .............................................................................................................. 440

Putting It All Together ....................................................................................................442
Writing the CreditCard Class................................................................................................................. 442
Writing the CardValidator SOAP Web Service....................................................................................... 442
Compiling and Packaging with Maven ................................................................................................. 443
Deploying on GlassFish......................................................................................................................... 444
Writing the Web Service Consumer...................................................................................................... 446
Generating Consumer’s Artifacts and Packaging with Maven ............................................................. 447
Running the Main Class........................................................................................................................ 449

Summary .......................................................................................................................450
■Chapter 15: RESTful Web Services ......................................................................451
Understanding RESTful Web Services ...........................................................................451
Resources and URIs.............................................................................................................................. 451
Representations.................................................................................................................................... 452
WADL .................................................................................................................................................... 453
HTTP ..................................................................................................................................................... 453

RESTful Web Services Specification..............................................................................458
A Brief History of REST ......................................................................................................................... 459
JAX-RS 1.1............................................................................................................................................ 459
What’s New in JAX-RS 1.1?.................................................................................................................. 459
Reference Implementation ................................................................................................................... 460

The REST Approach .......................................................................................................460

xv
www.it-ebooks.info

■ CONTENTS

From the Web to Web Services ............................................................................................................ 460
A Web-Browsing Experience ................................................................................................................ 460
Uniform Interface.................................................................................................................................. 461
Addressability...............................................................................................................................