Course Description

This EJB and JPA training course covers in-depth details of how to use these powerful Java EE technologies. Although previously difficult to program, EJB components in modern applications are simple yet provide many powerful features like transactional behavior and fine-grained security. Combined with the simplicity of the Java Persistence API using EJB and JPA technology together is an excellent synergy built into the Java EE standards. Using these technologies could help you create applications that leverage more of the Java EE standards and depend less on third party libraries like Spring and Hibernate. This course will show you how to use the features of both of these technologies, including some of the new features introduced in Java EE 6.



This class combines lecture with hands-on experience, and open ended discussion that will help the developer quickly understand the various backend technologies available with Java EE 6.

Upon completion of the course, students will:

  • Develop business logic layer using EJB 3.1
  • Use various EJB features like Singleton EJBs, Asynchronous EJBs, and EJB Timers
  • Persist data using Java Persistence (JPA)
  • Define relationships in JPA Entities
  • Query data using JPA Query Language and Criteria Queries
  • Use CDI (Contexts and Dependency Injection) to perform type-safe dependency injection of components


  • EJB Overview
  • Session Beans
  • Contexts and Dependency Injection (CDI)
  • Java Persistence API (JPA)
  • Transactions
  • Security
  • Message-Driven Beans



5 days


Target Audience

Software designers, developers and programmers new to Java EE or with J2EE 1.4 or earlier experience


Course Prerequisites

Participants should already have an understanding of Java programming. For those that require Java experience before this course the following course is suggested:

GTJ01 – Introduction to Java Using Eclipse


Suggested Follow on Courses

There are a number of options of suitable follow-on courses, depending on your business needs. Please contact us for further details.


Course Content

Chapter 1. Overview of Enterprise JavaBeans (EJB)

What are EJBs?

Distributed Transaction

Distributed Security

Distributed Computing

Main Characteristics of EJBs

EJB Container

EJB Client


Enterprise JavaBeans

Session Beans

Message-Driven Beans (MDBs)

Asynchronous Session EJBs

EJB Timers

EJB Lite

EJB Packaging

EJBs are Simple!

Chapter 2. Stateless Session EJB

Stateless Session Bean

Stateless Session Bean Components

Example: Business Interface

Example: Bean Class

Business Interface Details

Using an EJB in a Client

Portable JNDI Syntax

No-interface EJB

Stateless Session Bean Lifecycle

Stateless Session Bean Initialization

Chapter 3. Stateful Session EJB

Stateful Session Bean

Stateful Session Beans

Session Bean Lifecycle

Stateful Session Bean Lifecycle

Stateful Session Bean Example

Stateful Session Bean Client

Removing the Bean

Why Use Stateful Session EJBs?

Stateful Session EJB and Transactions

Chapter 4. Overview of Contexts and Dependency Injection

Once upon a time

What is CDI?

The JSRs

CDI Example

What Good is DI

Old Bean Scope

CDI Bean Scope

Other Advanced Features of CDI

CDI Implementation

Chapter 5. Defining CDI Beans

Requirements of Bean Classes

Bean Initialization Methods


Managed Bean Type

Using Beans with EL

Beans Have Default Names

Bean Scopes


Chapter 6. Dependency Injection in CDI

Injection Points


Typesafe Resolution


Using an @Qualifier


The Client Proxy

Chapter 7. Overview of Java Persistence API

Data Persistence

Java Persistence API


Session EJB vs JPA Entities


Persisting and Retrieving Data

Accessing Entities

EntityManager & Persistence Unit

Persistence Context

Entities – Example

persistence.xml – Hibernate Provider

persistence.xml – Open JPA Provider

persistence.xml – Toplink

Entity Instance Lifecycle

Creating EntityManager in Session EJB

Creating EntityManager in a Plain Java Class

Working With the EntityManager Interface

Transaction Basics

Chapter 8. JPA Entity Lifecycle

Entity Lifecycle

When is an Entity Managed or Detached?

Implementing the CRUD Pattern

Accessing Entities Using Stateless Session Beans

Inserting Data

Retrieving Data

Updating Data

Deleting Data

Merging Entities

Merging Entities (example)

Life-Cycle Callbacks

Example: Internal callback

External Callback Class

Listener Class – Example

Synchronizing with Databases

Entity Lookup

JPAQL (JPA Query Language)

Chapter 9. Java Persistence Query Language (JPA QL)

JPA Query Language

Basic JPAQL Syntax

Simple SELECT Examples

Example of Using JPAQL

The SELECT clause

Reading Data from Code

The WHERE Clause

Example Using JPAQL from Session EJB

Named Query

Multiple Named Queries


Bulk Updates

Bulk Delete

Running Native SQL Query

Native SELECT Query

Native SQL Delete and Insert

Named Native Query

Chapter 10. Basic JPA Entity Relationships

Relationship Between Entities

Anatomy of a Relationship

Foreign Key

Example Schema

One-to-One Unidirectional

Creating Entity Instances

Traversing the Relationship

The Cascade Behavior

One-to-One Bidirectional

Maintaining Bidirectional Links

Simplifying Bidirectional Links

Traversing the Relationship

Chapter 11. Complex Entity Relationships

One-To-Many and Many-To-One

Many-to-One Unidirectional

Creating Entity Instances

Traversing the Relationship

Modeling One-to-Many

Maintaining Bidirectional Links

Creating Entity Instances

Traversing the Relationship


Modeling Many-to-Many

Maintaining Bidirectional Links

Creating Entity Instances

Traversing Relationship

Modeling Unidirectional One-to-Many – @JoinTable

Modeling Unidirectional One-to-Many – @JoinColumn

Creating Entity Instances

Traversing the Relationship

Relationship in JPA QL

Fetching Optimizations

Lazy vs. Eager Initialization

Lazy Initialization

Various Problems with Lazy Initialization

Fetch Join Query

Chapter 12. JPA Entity Inheritance


Inheritance Example

Inheritance and Entities

Inheritance Strategies

Strategy 1: Single Table Per Class Hierarchy

Single Table Per Class Hierarchy

Table Structure: Single Table Per Class Hierarchy

Pros and Cons

Strategy 2: Table Per Concrete Class

Table Per Concrete Class

Table Per Concrete Class: JBoss Warning

Table Per Concrete Class

Table Structure: Table Per Concrete Class

Table Per Concrete Class: Pros and Cons

Strategy 3: Joined Subclasses

Joined Subclasses

Table Structure: Joined Subclasses

Joined Subclasses: Pros and Cons

Which Approach To Use?

Chapter 13. JPA Criteria Queries

JPA QL Limitations

Dynamic Queries with JPA Criteria API

Simple Criteria Query Example

The javax.persistence.criteria.CriteriaQuery Interface

The javax.persistence.criteria.CriteriaBuilder Interface

More Complex Example

Compounding Where Clause Predicates

Chapter 14. Message Driven EJBs

The Limitations of RMI/IIOP

Messaging to the Rescue

Message-Oriented Middleware as Messaging Platform

Messaging Domains




Java Message Service

JMS Programming: Sending a Message

JMS Programming: Receiving a Message

The JMS Interfaces

Introducing Message-Driven Beans

Message-Driven Beans, Big Picture

Message-Driven Bean’s Lifecycle

Message-Driven Beans in More Details

Message-Driven Bean Interfaces


Simple Message-Driven Bean Example



Load Balancing

Clustering and Topics

Clustering and Queues

Poison Messages

How the Programmer Can Avoid Poison Messages

How the System Administrator Can Avoid Poison Messages

Chapter 15. Asynchronous Session EJB

Previous Options

Asynchronous EJB Calls

Asynchronous Business Method Creation

Return Values

Example: Future<V> return type

Relationship With Java SE Concurrent Programming

Future<V> Methods Available

Asynchronous Client Models

Throwing Exceptions From Asynchronous Methods

Chapter 16. Singleton Session EJBs

Singleton Beans

Initializing Singletons at Startup

Singleton Dependence


Singleton Concurrency

Container Managed Concurrency

Bean Managed Concurrency

Access Timeout

Re-entrant Locking Behavior

Singletons in Clusters

Chapter 17. EJB Timer Service

Container Managed Timer Service

EJB Timer Use Cases

Interaction between Timer Service and EJB

Calendar-based Timer: Automatically Created

Scheduling Timer Execution

The @Schedule Annotation

@Schedule Attributes

@Schedule Attribute Syntax

Programmatic Timers

Timer Service API

The TimerService Interface

Calendar-Based Timer: Programmatically Created

Starting and Ending Programmatic Calendar Timers

Programmatically Creating a non-Calendar Timer

Getting All Timers

The Timeout Callback method

The Timer Interface

Timer and Transaction

Timer Example: Timer Using @Schedule

Timer Example: Timer Using ScheduleExpression

Timer Example: Timer By Duration

Non-persistent Timers

Non-persistent Timers in Clusters

Chapter 18. Transactions in EJB

Need for Transactions


ACID Properties

Transaction Components

Distributed Transactions

Distributed Transaction Components – Two Phase Commit

Java Transaction API (JTA)

EJB Transaction Basics

Transaction Propagation

Transaction Outcome

Container-Managed Transaction

Transaction Attributes

Container-Managed Transaction Settings

Interacting with Container-Managed Transactions

Container-Managed Transaction – Example

Application Exceptions in Container-Managed Transactions

Transaction Attributes Support

Bean-Managed Transaction Management Type

Chapter 19. EJB Security


How EJB Security Works: Set-Up

How EJB Security Works: Runtime

Example Security

Protecting Web Resources

Setting EJB Method Permissions

Defining Roles in the Deployment Descriptor

Defining Roles in Code

Specify Role Access in the Deployment Descriptor

Specify Role Access in Code

Disable Security Check

The Excludes List

Run As Identity

Setting Run As Role

Programmatic EJB Security

Programmatic EJB Security Example

Role Reference

Chapter 20. EJB Testing

Overview of the EJB Embeddable Enterprise Bean Container

Embeddable Container Features

Initializing the Embeddable Container

Using the Embeddable Container

The System in a Nutshell

EJB Unit Testing

Unit Testing with JUnit Testing Framework


See more Java courses