Introduction
Enterprise Java Beans (EJB) is a key part of Java EE, which is designed to make large-scale app development easier. It's a server-side component that holds the main business logic of an application. EJB helps developers focus on solving business problems instead of worrying about low-level details. This technology manages things like security, transactions, & persistence automatically.

In this article, we'll discuss the types of EJB, when to use them, & their advantages & disadvantages. We will also learn how to implement this with the help of relevant examples.
Types of Enterprise Java Beans
Enterprise Java Beans (EJB) come in three main types. Each type serves a different purpose in building enterprise applications. Let's break them down one by one:
Session Bean
Session Beans are the workhorses of EJB. They do the main tasks in your application. Think of them as objects that live on the server & do things when a client asks. There are two kinds of Session Beans:
a) Stateless Session Beans
These beans don't keep any info between method calls. They're like fresh workers who start from scratch each time. They're good for tasks that don't need to remember anything.
Example :
@Stateless
public class CalculatorBean implements Calculator {
public int add(int a, int b) {
return a + b;
}
}
b) Stateful Session Beans
These beans remember info between method calls. They're like workers who keep notes. They're useful when you need to track something across multiple steps.
Example :
@Stateful
public class ShoppingCartBean implements ShoppingCart {
private List<String> items = new ArrayList<>();
public void addItem(String item) {
items.add(item);
}
public List<String> getItems() {
return items;
}
}
Session Beans are great for handling business logic, calculations, & operations that don't involve storing data permanently.
Message Driven Bean
Message Driven Beans (MDBs) are different from other EJBs. They don't talk directly to clients. Instead, they listen for messages from a messaging system. When a message comes in, the MDB wakes up & does its job.
MDBs are good for tasks that don't need an immediate response. They help make systems that can handle lots of work without slowing down.
Let’s see how they work:
-
A client sends a message to a queue or topic.
-
The MDB is listening to that queue or topic.
-
When a message arrives, the container calls the MDB's onMessage method.
- The MDB processes the message.
Here's an example of a simple Message Driven Bean:
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "myQueue")
})
public class MyMDB implements MessageListener {
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
String text = ((TextMessage) message).getText();
System.out.println("Received: " + text);
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
In this example, the MDB listens to a queue named "myQueue". When a message comes in, it prints the message text.
MDBs are great for building systems that need to handle a lot of tasks in the background.
They're often used for things like:
-
Processing orders in an e-commerce system
-
Sending emails
-
Updating databases based on events
- Generating reports
MDBs help make systems more scalable & reliable. They can keep working even if parts of the system are down or busy.
Entity Bean
Entity Beans represent data in a database. They're Java objects that map directly to database tables. Each Entity Bean instance matches a row in a table.
It's important to note that Entity Beans are no longer part of the EJB 3.0 specification. They've been replaced by Java Persistence API (JPA) entities. However, understanding Entity Beans is still useful, especially when working with older systems.
Here's how Entity Beans work:
-
They represent persistent data stored in a database.
-
Each field in the bean matches a column in the database table.
- The EJB container handles reading & writing data to the database.
Let's look at an example of what an Entity Bean might look like:
@Entity
@Table(name = "CUSTOMERS")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "EMAIL")
private String email;
// Getters & setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
In this example:
-
The @Entity annotation marks this class as an entity bean.
-
The @Table annotation specifies which database table this bean maps to.
-
Each field has a @Column annotation to map it to a database column.
- The @Id annotation marks the primary key field.
Entity Beans (now JPA entities) are useful for:
-
Representing business data in your application
-
Automatically saving & loading data from the database
- Keeping your data consistent across your application
While Entity Beans are outdated, the concept of mapping Java objects to database tables is still very important in Java enterprise development.