EJB 3 in Action
Debu Panda, Reza Rahman, Derek Lane
  • April 2007
  • ISBN 9781933988344
  • 712 pages
  • printed in black & white
This title is out of print and no longer for sale.

... this technical book is surprisingly entertaining.

King Y. Wang, Oracle Canada

Second edition of this book is available

EJB 3 in Action is a fast-paced tutorial for both novice and experienced Java developers. It will help you learn EJB 3 and the JPA quickly and easily. This comprehensive, entirely new EJB 3 book starts with a tour of the EJB 3 landscape. It then moves quickly into core topics like building business logic with session and message-driven beans. You'll find four full chapters on the JPA along with practical code samples, design patterns, performance tuning tips, and best practices for building and deploying scalable applications.

Table of Contents detailed table of contents



about this book

Part 1 Overview of the EJB landscape

1. What’s what in EJB 3

1.1. EJB overview

1.1.1. EJB as a component

1.1.2. EJB as a framework

1.1.3. Layered architectures and EJB

1.1.4. Why choose EJB 3?

1.2. Understanding EJB types

1.2.1. Session beans

1.2.2. Message-driven beans

1.2.3. Entities and the Java Persistence API

1.3. Getting inside EJB

1.3.1. Accessing EJB services: the EJB container

1.3.2. Accessing JPA services: the persistence provider

1.3.3. Gaining functionality with EJB services

1.4. Renaissance of EJB

1.4.1. HelloUser Example

1.4.2. Simplified programming model

1.4.3. Annotations instead of deployment descriptors

1.4.4. Dependency injection vs. JNDI lookup

1.4.5. Simplified persistence API

1.4.6. Unit-testable POJO components

1.4.7. EJB 3 and Spring

1.5. Summary

2. A first taste of EJB

2.1. New features: simplifying EJB

2.1.1. Replacing deployment descriptors with annotations

2.1.2. Introducing dependency injection

2.2. Introducing the ActionBazaar application

2.2.1. Starting with the architecture

2.2.2. An EJB 3-based solution

2.3. Building business logic with session beans

2.3.1. Using stateless beans

2.3.2. The stateless bean client

2.3.3. Using stateful beans

2.3.4. A stateful bean client

2.4. Messaging with message-driven beans

2.4.1. Producing a billing message

2.4.2. Using the order billing message processor MDB

2.5. Persisting data with EJB 3 JPA

2.5.1. Working with the Java Persistence API

2.5.2. Using the EntityManager

2.6. Summary

Part 2 Building business logic with EJB

3. Building business logic with session beans

3.1. Getting to know session beans

3.1.1. Why use session beans?

3.1.2. Session beans: the basics

3.1.3. Understanding the programming rules

3.1.4. Conversational state and session bean types

3.1.5. Bean lifecycle callbacks

3.2. Stateless session beans

3.2.1. The BidManagerBean example

3.2.2. Using the @Stateless annotation

3.2.3. Specifying bean business interfaces

3.2.4. Using bean lifecycle callbacks

3.3. Stateful session beans

3.3.1. Additional programming rules

3.3.2. The BidderAccountCreatorBean example

3.3.3. Business interfaces for stateful beans

3.3.4. Stateful bean lifecycle callbacks

3.4. Session bean clients

3.4.1. Using the @EJB annotation

3.4.2. Injection and stateful session beans

3.5. Performance considerations for stateful beans

3.5.1. Using stateful session beans effectively

3.5.2. Stateful session bean alternatives

3.6. Session bean best practices

3.7. Summary

4. Messaging and developing MDBs

4.1. Messaging concepts

4.1.1. Message-oriented middleware

4.1.2. Messaging in ActionBazaar

4.1.3. Messaging models

4.2. Introducing Java Messaging Service

4.2.1. Developing the JMS message producer

4.2.2. The JMS message interface

4.3. Working with message-driven beans

4.3.1. Why use MDBs?

4.3.2. Programming rules

4.3.3. Developing a message consumer with MDB

4.3.4. Using the @MessageDriven annotation

4.3.5. Implementing the MessageListener

4.3.6. Using ActivationConfigProperty

4.3.7. Using bean lifecycle callbacks

4.3.8. Sending JMS messages from MDBs

4.3.9. Managing MDB transactions

4.4. MDB best practices

4.5. Summary

5. Learning advanced EJB concepts

5.1. EJB internals

5.1.1. EJB behind the scenes

5.1.2. EJB context: accessing the runtime environment

5.2. Accessing resources using DI and JNDI

5.2.1. Resource injection using @Resource

5.2.2. The @Resource annotation in action

5.2.3. Looking up resources and EJBs

5.3. AOP in the EJB world: interceptors

5.3.1. What is AOP?

5.3.2. What are interceptors?

5.3.3. Specifying interceptors

5.3.4. Implementing business interceptors

5.3.5. Lifecycle callback methods in the interceptor class

5.4. Scheduling: the EJB 3 timer service

5.4.1. What are timers?

5.4.2. Using the timer service

5.4.3. When to use EJB timers

5.5. Summary

6. Transactions and security

6.1. Understanding transactions

6.1.1. A transactional solution in ActionBazaar

6.1.2. ACID properties

6.1.3. Transaction management internals

6.1.4. Two-phase commit

6.1.5. Transaction management in EJB

6.2. Container-managed transactions

6.2.1. Snag-It ordering using CMT

6.2.2. The @TransactionManagement annotation

6.2.3. The @TransactionAttribute annotation

6.2.4. Marking a CMT for rollback

6.2.5. Transaction and exception handling

6.3. Bean-managed transactions

6.3.1. Snag-It ordering using BMT

6.3.2. Getting a UserTransaction

6.3.3. Using UserTransaction

6.3.4. The pros and cons of BMT

6.4. Exploring EJB security

6.4.1. Authentication vs. authorization

6.4.2. Users, groups, and roles

6.4.3. A security problem in ActionBazaar

6.4.4. EJB 3 and Java EE security

6.4.5. Declarative security

6.4.6. Using EJB programmatic security

6.5. Summary

Part 3 Diving into the Java persistence API (JPA)

7. Implementing domain models

7.1. Domain modeling and the JPA

7.1.1. Introducing domain models

7.1.2. The ActionBazaar problem domain

7.1.3. Domain model actors

7.1.4. The EJB 3 Java Persistence API

7.1.5. Domain objects as Java classes

7.2. Implementing domain objects with JPA

7.2.1. The @Entity annotation

7.2.2. Persisting entity data

7.2.3. Specifying entity identity

7.2.4. The @Embeddable annotation

7.3. Entity relationships

7.3.1. @OneToOne

7.3.2. @OneToMany and @ManyToOne

7.3.3. @ManyToMany

7.4. Summary

8. Object-relational mapping

8.1. The impedance mismatch

8.1.1. Mapping objects to databases

8.1.2. Introducing O/R mapping

8.2. Mapping entities

8.2.1. Specifying the table

8.2.2. Mapping the columns

8.2.3. Using @Enumerated

8.2.4. Mapping CLOBs and BLOBs

8.2.5. Mapping temporal types

8.2.6. Mapping an entity to multiple tables

8.2.7. Generating primary keys

8.2.8. Mapping embeddable classes

8.3. Mapping entity relationships

8.3.1. Mapping one-to-one relationships

8.3.2. One-to-many and many-to-one

8.3.3. Many-to-many

8.4. Mapping inheritance

8.4.1. Single-table strategy

8.4.2. Joined-tables strategy

8.4.3. Table-per-class strategy

8.4.4. Mapping polymorphic relationships

8.5. Summary

9. Manipulating entities with EntityManager

9.1. Introducing the EntityManager

9.1.1. The EntityManager interface

9.1.2. The lifecycle of an entity

9.1.3. Persistence contexts, scope, and the EntityManager

9.1.4. Using the EntityManager in ActionBazaar

9.2. Creating EntityManager instances

9.2.1. Container-managed EntityManagers

9.2.2. Application-managed EntityManager

9.3. Managing persistence operations

9.3.1. Persisting entities

9.3.2. Retrieving entities by primary key

9.3.3. Updating entities

9.3.4. Deleting entities

9.3.5. Controlling updates with flush

9.3.6. Refreshing entities

9.4. Entity lifecycle listeners

9.4.1. Using an entity listener

9.4.2. Default listener classes

9.4.3. Listener class execution order and exclusion

9.5. Entity operations best practices

9.6. Summary

10. Using the query API and JPQL to retrieve entities

10.1. Introducing the query API

10.1.1. The big picture

10.1.2. Anatomy of a query

10.1.3. Defining named queries

10.2. Executing the queries

10.2.1. Creating a query instance

10.2.2. Working with the Query interface

10.2.3. Specifying query hints

10.3. Introducing JPQL

10.3.1. Defining statement types

10.3.2. Using the FROM clause

10.3.3. Conditional expressions and operators

10.3.4. Working with JPQL functions

10.3.5. Using a SELECT clause

10.3.6. Using aggregations

10.3.7. Ordering the query result

10.3.8. Using subqueries

10.3.9. Joining entities

10.3.10. Bulk updates and deletes

10.4. Native SQL queries

10.4.1. Using dynamic queries with native SQL

10.4.2. Using a named native SQL query

10.5. Summary

Part 4 Putting EJB 3 into action

11. Packaging EJB 3 applications

11.1. Packaging your applications

11.1.1. Dissecting the EAR file

11.1.2. Loading the EAR module

11.2. Exploring class loading

11.2.1. Class-loading basics

11.2.2. Exposing the classic parent delegation model

11.2.3. Class loading in Java EE applications

11.2.4. Dependencies between Java EE modules

11.3. Packaging session and message-driven beans

11.3.1. Packaging EJB-JAR

11.3.2. Deployment descriptors vs. annotations

11.3.3. Overriding annotations with deployment descriptors

11.3.4. Specifying default interceptor settings

11.3.5. Using vendor-specific annotations and descriptors

11.4. Packaging entities

11.4.1. Exposing the persistence module

11.4.2. Describing the persistence module with persistence.xml

11.4.3. Performing O/R mapping with orm.xml

11.5. Best practices and common deployment issues

11.5.1. Packaging and deployment best practices

11.5.2. Troubleshooting common deployment problems

11.6. Summary

12. Effectively integrating EJB 3 across your application tiers

12.1. Design patterns and web technologies

12.1.1. Presentation tier

12.1.2. Using the Entity Access Object pattern

12.1.3. Visiting the Session Façade pattern

12.2. Accessing session beans from the web tier

12.2.1. Accessing session beans using dependency injection

12.2.2. Referencing session beans from helper classes

12.2.3. Dealing with transactions

12.2.4. Working with stateful session beans

12.3. Using JPA from the web tier

12.3.1. Using a container-managed entity manager

12.3.2. Using an application-managed EntityManager with JTA transactions

12.3.3. Accessing an application-managed EntityManager outside the container

12.4. Summary

13. Taming wild EJBs: performance and scalability

13.1. Handling entity locking issues

13.1.1. Understanding locking types

13.1.2. Optimistic locking and entity versioning

13.1.3. EntityManager and lock modes

13.2. Improving entity performance

13.2.1. Remodeling and schema redesign

13.2.2. Tuning the JDBC layer

13.2.3. Reducing database operations

13.2.4. Improving query performance

13.2.5. Caching

13.3. Improving performance of EJB 3 components

13.3.1. Session bean performance

13.3.2. Improving MDB performance

13.4. Clustering EJB applications

13.4.1. Collocated architecture

13.4.2. Load-balancing stateless session beans

13.4.3. Clustering stateful session beans

13.4.4. Entities and clustered cache

13.5. Summary

Part 5 Migration and Interoperability Issues

14. Migrating to EJB 3

14.1. Backward compatibility and interoperability with EJB 2

14.1.1. Packaging EJB 2 and EJB 3 together

14.1.2. Invoking EJB 2 from EJB 3

14.1.3. Using EJB 3 from EJB 2

14.2. Migrating session beans

14.2.1. Converting interface and bean classes

14.2.2. Resource usage

14.2.3. Transactions and security settings

14.2.4. Client applications

14.3. Migrating message-driven beans

14.4. Migrating CMP 2 entity beans to the EJB 3 JPA

14.4.1. Redesign your domain model

14.4.2. Using DTOs as entities

14.4.3. Entity bean classes and interfaces

14.4.4. Client applications

14.5. Migrating JDBC DAOs to use the EJB 3 JPA

14.5.1. Identifying entities

14.5.2. Reworking a DAO implementation class to use the EntityManager API

14.5.3. Swapping SQL queries for JPQL

14.6. Helping O/R frameworks to use the EJB 3 JPA

14.7. Approaches to migration

14.7.1. Strategies

14.7.2. Manual vs. automated

14.8. Summary

15. Exposing EJBs as web services

15.1. What is a web service?

15.1.1. Identifying web service components

15.1.2. Web services styles

15.1.3. Approaches to developing web services

15.2. JAX-WS: Java EE 5 web services platform

15.2.1. Introducing the web services platform

15.2.2. Why choose EJB over a POJO for a web service?

15.3. Developing EJB web services with JAX-WS 2.0

15.3.1. Using the @WebService annotation

15.3.2. Specifying the web service style with @SOAPBinding

15.3.3. Using @WebMethod

15.3.4. Using the @WebParam annotation

15.3.5. Using the @WebResult annotation

15.3.6. Using @OneWay and @HandlerChain

15.4. Accessing a web service from an EJB

15.4.1. Accessing the PlaceBid web service

15.4.2. EJB as a web service client

15.5. Best practices for web service development

15.6. Summary

16. EJB 3 and Spring

16.1. Introducing the Spring framework

16.1.1. Benefits of the Spring framework

16.1.2. The inversion of control principle

16.1.3. The separation of concerns principle

16.2. Using JPA with Spring

16.2.1. Building JPA EAOs for Spring

16.2.2. Configuring Spring to use the JPA

16.3. Combining the power of EJB 3 and Spring

16.3.1. Developing Spring-enabled EJBs

16.3.2. Using session beans from Spring beans

16.4. Summary

Appendix A: RMI and JNDI

Appendix B: Reviewing relational databases

Appendix C: Annotations reference

Appendix D: Deployment descriptors reference

Appendix E: Installing and configuring the Java EE 5 SDK



About the Technology

EJB 2 is widely used but it comes at a cost—procedural, redundant code. EJB 3 is a different animal. By adopting a POJO programming model and Java 5 annotations, it dramatically simplifies enterprise development. A cool new feature, its Java Persistence API, creates a standard for object-relational mapping. You can use it for any Java application, whether inside or outside the EJB container. With EJB 3 you will create true object-oriented applications that are easy to write, maintain and extend.

What's inside

  • Dependency Injection and Interceptors
  • Domain modeling and persisting entities with the JPA and its query language
  • Using EJB 3 across application tiers and web services
  • Integrating with Spring
  • Migrating from EJB 2, JDBC, and other O-R frameworks

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 with Tripod Technologies, an IT solutions company focusing on Java EE in the Baltimore-NYC corridor. Reza has been working with Java as a language and Java EE as a platform since their inception in the mid-nineties. He has worked with both Enterprise Java Beans and open source tools like Spring and Hibernate, developing enterprise systems in the software, consulting, financial, Telecommunications, and manufacturing industries.

Derek Lane is the CTO of Semantra, Inc. He has worn various hats in his career including mentor, coach, architect, manager, developer, trainer, methodologist, and resident open source zealot. Lane is a contributor to projects of various shape and size as author, presenter, and technical reviewer. Lane is the founder of both the Oklahoma City Java User Group (OKCJUG) and the Dallas/Fort Worth, Texas MicroJava User Group; and has been active as a member, presenter, and mentor for over a decade at various technology user groups across the Midwest and Southern U.S.