About this Book – EJB 3 in Action

About this Book

EJB 3 is meant to recast Java server-side development into a mold you might not expect. And so have we tried to make this an EJB book you might not anticipate.

Most server-side Java books tend to be serious affairs—heavy on theory, slightly preachy, and geared toward the advanced developer. Nine out of ten EJB 2.x books follow this pattern. While we easily fit the stereotype of geeks and aren’t the funniest comedians or entertainers, we have tried to add some color to our writing to keep this book as lighthearted and down-to-earth as possible. The tone is intended to be friendly, conversational, and informal. We made a conscious effort to drive the chapter content with examples that are close to the real world problems you deal with every day. In most cases, we introduce a problem that needs to be solved, show you the code to solve it using EJB 3, and explore features of the technology using the code as a crutch.

We do cover theory when it is unavoidable and we don’t assume that you have a Ph.D. in computer science. We try to avoid theory for theory’s sake and try to make the discussion as lively, and short, as we can make it. The goal of this book is to help you learn EJB 3 quickly and effectively, not to be a comprehensive reference book. We don’t cover features you are unlikely to use. Instead, we provide deep coverage of the most useful EJB 3 features, discuss various options so you can make educated choices, warn you about common pitfalls, and tell you about battle-hardened best practices.

If you’ve picked up this book, it is unlikely you are a complete newcomer to Java. We assume you’ve done some work in Java, perhaps in the form of web development using a presentation tier technology like JSF, Struts, JSP, or Servlets. If you come from the client-side end of the spectrum using technologies like Swing and AWT, don’t worry. A web development background isn’t a requirement for EJB. We do assume you are familiar with database technologies such as JDBC, and have at least a casual familiarity with SQL. We don’t assume you are familiar with middleware-centric technologies like Spring, Hibernate, TopLink, JDO, iBATIS, or AspectJ. You don’t need to be an EJB 2.x expert to pick up this book. We don’t even assume you know any of the Java EE technologies that EJB is dependent on, such as the Java Naming and Directory Interface (JNDI), Java Remote Method Invocation (RMI), or the Java Messaging Service (JMS). In fact, we assume you are not familiar with middleware concepts like remoting, pooling, concurrent programming, security, or distributed transactions. This book is ideally suited for a Java developer with a couple of years’ experience who is curious about EJB 3. By the same token, there is enough depth here to keep an EJB 2.x or Spring/Hibernate veteran engaged. Familiar material is placed in a logical sequence so that it can easily be skipped.

You might find this book different from others in one more important way. EJB is a server-side middleware technology. This means that it doesn’t live in a vacuum and must be integrated with other technologies to fulfill its mission. Throughout the book, we talk about how EJB 3 integrates with technologies like JSF, JSP, Servlets, Ajax, and even Swing-based Java SE clients. We also talk about how EJB 3 aligns with complementary technologies like Spring.

This book is about EJB 3 as a standard, not a specific application server technology. For this reason, we will avoid tying our discussion around any specific application server implementation. Instead, the code samples in this book are designed to run with any EJB 3 container or persistence provider. The website accompanying this book at www.manning.com/panda will tell you how you can get the code up and running in GlassFish and Oracle Application Server 10g. Maintaining the application server-specific instructions on the publisher’s website instead of in the book will allow us to keep the instructions up-to-date with the newest implementation details.


This book is divided into five parts.

Part 1 provides an overview of EJB. Chapter 1 introduces EJB 3 and EJB types and makes the case for EJB 3. Chapter 2 explores core concepts such as metadata annotations, dependency injection, and provides code examples of each EJB type.

Part 2 covers the building of business logic with session beans and MDB. Chapter 3 dives into the details of session beans and outlines best practices. Chapter 4 gives a quick introduction to messaging, JMS, and covers MDB in detail. Chapter 5 covers advanced topics such as dependency injection, interceptors, and timers. Chapter 6 discusses transaction and security.

Part 3 provides in-depth coverage of the EJB 3 Java Persistence API. Chapter 7 introduces concepts on domain modeling and describes implementing domain models with JPA. Chapter 8 covers object-relational mapping with JPA. Chapter 9 provides in-depth coverage manipulating entities using EntityManager API. Chapter 10 covers querying entities using Query API and JPQL.

Part 4 provides guidelines for effectively using EJB 3 in your enterprise applications. Chapter 11 discusses packaging of EJBs and entities. It introduces all XML descriptors. Chapter 12 covers using EJB 3 design patterns and JPA from other application tiers. Chapter 13 turns to advanced topics such as entity locking and performance tuning of EJB 3 applications.

Part 5 looks at interoperability and integration issues with EJB 3 and other frameworks. Chapter 14 covers interoperability with EJB 2 and migration of EJB 2 applications to EJB 3. Chapter 15 introduces web services and discusses web services applications with EJB 3 and JAX WS 2.0. Chapter 16 uncovers how you can integrate EJB 3 with the Spring framework to build great enterprise applications.

The book has five appendixes. Appendix A is a tutorial on JNDI and RMI and appendix B provides a primer to databases. Appendixes C and D cover references to annotations and XML descriptors. We also provide instructions on how to install and configure Java EE RI (Glassfish) and how to deploy the code samples in appendix E.

Source Code Downloads

In addition to the setup instructions for the Java EE 5 Reference Implementation server based on Glassfish and the Oracle Application Server, the publisher’s website houses all of the source code presented in this book. The source code for each chapter is downloadable as a separate zip file, each one containing instructions on how to deploy the code to an application server and get it running. You can download the code from the book’s web page: www.manning.com/panda or www.manning.com/EJB3inAction.

Source Code Conventions

Because of the example-driven style of this book, the source code was given a great deal of attention. Larger sections of code in the chapters are presented as their own listings. All code is formatted using the fixed-width Courier font for visibility. All inside code, such as XML element names, method names, Java type names, package names, variable names, and so on are also formatted using the Courier font. Some code is formatted as Courier Bold to highlight important sections. In some cases, we’ve abbreviated the code to keep it short and simple. In all cases, the full version of the abbreviated code is contained in the downloadable zip files. We encourage you to set up your development environment for each chapter before you begin reading it. The setup instructions for the development environment are also included on the website.

Author Online

Purchase of EJB 3 in Action includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/panda. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the Authors

DEBU PANDA is a Lead Product Manager of the Oracle Application Server development team where he drives development of the Java EE container. He has more than 15 years of experience in the IT industry and has published numerous articles on enterprise Java technologies in several magazines and has presented at many conferences. His J2EE-focused weblog can be found at debupanda.com.

REZA RAHMAN is an architect at Tripod Technologies. Reza has been working with Java EE since its inception in the mid-nineties. He has developed enterprise systems in the publishing, financial, telecommunications, and manufacturing industries and has worked with Enterprise Java Beans, Spring, and Hibernate.

DEREK LANE is the CTO for Semantra, Inc. He has over 20 years’ experience in the IT arena. He is the founder of both the Dallas/Fort Worth, Texas MicroJava User Group and the Oklahoma City Java User Groups, and is active in numerous technology groups in the southwestern United States.

About the Title

By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science, the things people remember are things they discover during self-motivated exploration.

Although no one at Manning is a cognitive scientist, we are convinced that for learning to become permanent it must pass through stages of exploration, play, and, interestingly, re-telling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action guide is that it is example-driven. It encourages the reader to try things out, to play with new code, and explore new ideas.

There is another, more mundane, reason for the title of this book: our readers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it. They need books that aid them in action. The books in this series are designed for such readers.

About the Cover Illustration

The figure on the cover of EJB 3 in Action is a “Russian Girl with Fur,” taken from a French travel book, Encyclopedie des Voyages by J. G. St. Saveur, published in 1796. Travel for pleasure was a relatively new phenomenon at the time and travel guides such as this one were popular, introducing both the tourist as well as the armchair traveler to the inhabitants of other regions of France and abroad.

The diversity of the drawings in the Encyclopedie des Voyages speaks vividly of the uniqueness and individuality of the world’s towns and provinces just 200 years ago. This was a time when the dress codes of two regions separated by a few dozen miles identified people uniquely as belonging to one or the other. The travel guide brings to life a sense of isolation and distance of that period and of every other historic period except our own hyperkinetic present.

Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.

We at Manning celebrate the inventiveness, the initiative, and the fun of the computer business with book covers based on the rich diversity of regional life two centuries ago brought back to life by the pictures from this travel guide.