Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Enterprise JavaBeans (EJB): Creating Beans and Session/Entity Types

Lesson 38 of 46 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

Enterprise JavaBeans (EJB): Creating Beans and Session/Entity Types

What is a JavaBean?

A JavaBean is a reusable, plain Java class following simple conventions so that tools, frameworks, and IDEs can inspect and manipulate it automatically.

JavaBean Conventions

  1. Class must have a public no-argument constructor
  2. Properties are private, accessed via public getter/setter methods
  3. The class should be Serializable
import java.io.Serializable;

public class Student implements Serializable {
    private String name;
    private int age;

    public Student() { } // no-arg constructor

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
}

JavaBean Properties

Property TypeDescription
SimpleSingle value with getX() / setX()
IndexedArray-valued, with getX(int i) / setX(int i, value)
BoundNotifies listeners (PropertyChangeListener) when changed
ConstrainedAllows listeners to veto a change (VetoableChangeListener)

Enterprise JavaBeans (EJB)

EJB is a server-side component architecture for building scalable, transactional, and secure distributed business applications. EJB components run inside an EJB container (part of a Java EE / Jakarta EE application server), which supplies transaction management, security, concurrency, and remote access — so the developer focuses on business logic only.

Types of Enterprise Beans

TypeLifespanStatePurpose
Stateless Session BeanPer method callNo client-specific state retainedReusable business operations (e.g., a calculator, a mailer)
Stateful Session BeanTied to one client's conversationRetains state across callsMulti-step workflows (e.g., shopping cart)
Entity BeanPersists beyond the application (backed by a database row)Represents persistent dataObject representation of a database record

Creating a Stateless Session Bean

import javax.ejb.Stateless;

@Stateless
public class CalculatorBean implements CalculatorRemote {
    public int add(int a, int b) {
        return a + b;
    }
}

// Remote business interface
public interface CalculatorRemote {
    int add(int a, int b);
}

Creating a Stateful Session Bean

import javax.ejb.Stateful;
import java.util.ArrayList;
import java.util.List;

@Stateful
public class ShoppingCartBean implements ShoppingCartRemote {
    private List<String> items = new ArrayList<>();

    public void addItem(String item) {
        items.add(item);           // remembered for this client's session
    }

    public List<String> getItems() {
        return items;
    }
}

Entity Beans

An Entity Bean represents persistent business data stored in a database — each instance typically maps to one row of a table. Modern Java EE replaces the old EJB 2.x Entity Bean model with JPA (@Entity), but the concept is the same:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {
    @Id
    private int empId;
    private String name;
    private double salary;

    // getters and setters
}

Session Bean vs Entity Bean

FeatureSession BeanEntity Bean
RepresentsA process / business logicPersistent data
LifespanShort-lived (per call or per client)Long-lived (tied to DB record)
PersistenceNot persistedBacked by the database
ExampleOrderProcessorBeanEmployee, Customer entity
Key Takeaway: A JavaBean is just a class with a no-arg constructor and getters/setters. Enterprise JavaBeans extend that idea into server-managed components — Stateless beans for reusable logic, Stateful beans for per-client conversations, and Entity beans (now largely JPA @Entity) for persistent data.