Part 1. Overview of the EJB landscape
This book is about Enterprise Java Beans (EJB) 3, and covers up to the EJB 3.2 specification. The goal of EJB 3.2 is to continue to evolve the EJB specification to be a complete solution for all Enterprise business needs and to improve the EJB architecture by reducing its complexity from the developerâs point of view.
Part 1 presents EJB 3 as a powerful, highly usable platform worthy of its place as the business component development standard for mission-critical Enterprise development. Weâll introduce the Java Persistence API (JPA 2.1), a Java EE technology that aims to standardize Java ORM and works hand-in-hand with EJB 3. Weâll also take a quick look at Contexts and Dependency Injection for Java (CDI 1.1), the next-generation generic type-safe dependency injection technology for Java EE.
In chapter 1 we introduce the pieces that make up EJB 3, touching on the unique strengths EJB has as a development platform and the new features that promote productivity and ease of use. We even throw in a âHello Worldâ example.
In chapter 2 we provide more realistic code samples and introduce the Action-Bazaar application, an imaginary Enterprise system developed throughout the book. Weâll try to give you a feel for how EJB 3 looks as quickly and easily as possible. Be ready for a lot of code!
Chapter 1. Whatâs what in EJB 3
This chapter covers
- The EJB container and its role in Enterprise applications
- The different types of Enterprise Java Beans (EJBs)
- Closely related technologies such as the Java Persistence API (JPA)
- The different EJB runtime environments
- Innovations started with EJB 3
- New changes with EJB 3.2
One day, when God was looking over his creatures, he noticed a boy named Sadhu whose humor and cleverness pleased him. God felt generous that day and granted Sadhu three wishes. Sadhu asked for three reincarnationsâone as a ladybug, one as an elephant, and the last as a cow. Surprised by these wishes, God asked Sadhu to explain himself. The boy replied, âI want to be a ladybug so that everyone in the world will admire me for my beauty and forgive the fact that I do no work. Being an elephant will be fun because I can gobble down enormous amounts of food without being ridiculed. Iâll like being a cow the best because Iâll be loved by all and useful to mankind.â God was charmed by these answers and allowed Sadhu to live through the three incarnations. He then made Sadhu a morning star for his service to humankind as a cow.
EJB too has lived through three major incarnations. When it was first released, the industry was dazzled by its innovations. But like the ladybug, EJB 1 had limited functionality. The second EJB incarnation was almost as heavy as the largest of our beloved pachyderms. The brave souls who couldnât do without its elephant power had to tame the awesome complexity of EJB 2. And finally, in its third incarnation, EJB has become much more useful to the huddled masses, just like the gentle bovine thatâs sacred for Hindus and respected as a mother whose milk feeds us well.
A lot of hard work from a lot of good people made EJB 3 simple and lightweight without sacrificing Enterprise-ready power. EJB components can now be Plain Old Java Objects (POJOs) and look a lot like code in a âHello Worldâ program. In the following chapters weâll describe a star among frameworks with increasing industry adoption.
Weâve strived to keep this book practical without skimping on content. The book is designed to help you learn EJB 3 quickly and easily without neglecting the basics. Weâll also dive into deep waters, sharing all the amazing sights weâve discovered and warning about any lurking dangers.
In the Java world EJB is an important and uniquely influential technology radically transformed in version 3. Weâll spend little time with EJB 2. You probably either already know earlier versions of EJB or are completely new to it. Spending too much time on previous versions is a waste of time. EJB 3 and EJB 2 have very little in common, and EJB 3.2 now makes support for EJB 2 optional. But if youâre curious about EJB 2, we encourage you to pick up one of the many good books on the previous versions of EJB.
In this chapter weâll tell you whatâs what in EJB 3, explain why you should consider using it, and outline the significant improvements the newest version offers, such as annotations, convention-over-configuration, and dependency injection. Weâll build on the momentum of this chapter by jumping into code in chapter 2. Letâs start with a broad overview of EJB.
1.1. EJB overview
The first thing that should cross your mind while evaluating any technology is what it really gives you. Whatâs so special about EJB? Beyond a presentation-layer technology like JavaServer Pages (JSP), JavaServer Faces (JSF), or Struts, couldnât you create your web application using the Java language and some APIs like Java Database Connectivity (JDBC) for database access? You couldâif deadlines and limited resources werenât realities. Before anyone dreamed up EJB, this is exactly what people did. The resulting long hours proved that youâd spend a lot of time solving very common system-level problems instead of focusing on the real business solution. These experiences emphasized that there are common solutions for common development problems. This is exactly what EJB brings to the table. EJB is a collection of âcannedâ answers to common server application development problems, as well as a roadmap to common server component patterns. These canned solutions or services are provided by the EJB container. To access these services, you build specialized components using declarative and programmatic EJB APIs and deploy them into the container.
1.1.1. EJB as a component model
In this book, EJBs refer to server-side components that you can use to build the business component layer of your application. Some developers associate the term component with developing complex and heavyweight CORBA or Microsoft COM+ code. In the brave new world of EJB 3, a component is what it ought to beânothing more than a POJO with some special powers. More importantly, these powers stay invisible until theyâre needed and donât distract from the real purpose of the component. Youâll see this firsthand throughout this book, especially starting in chapter 2.
To use EJB services, your component must be declared to be a recognized EJB component type. EJB recognizes two specific types of components: session beans and message-driven beans. Session beans are further subdivided into stateless session beans, stateful session beans, and singletons. Each component type has a specialized purpose, scope, state, lifecycle, and usage pattern in the business logic tier. Weâll discuss these component types throughout the rest of the book, particularly in part 2. For data CRUD (create, read, update, delete) operations in the persistence tier, weâll talk about JPA entities and their relationship with EJBs in detail in part 3. As of EJB 3.1, all EJBs are managed beans. Managed beans are basically any generic Java object in a Java EE environment. Contexts and Dependency Injection (CDI) allows you to use dependency injection with all managed beans, including EJBs. Weâll explore CDI and managed beans further in part 3.
1.1.2. EJB component services
As we mentioned, the canned services are the most valuable part of EJB. Some of the services are automatically attached to recognize components because they make a lot of sense for business logic-tier components. These services include dependency injection, transactions, thread safety, and pooling. To use most services, you must declare you want them using annotations/XML or by accessing programmat...