Difference Between Session and Entity Beans

By: Paul Allen and Joseph Bambara Emailed: 1677 times Printed: 2158 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Here we review two of the three types of EJBs: session and entity beans.

Session and Entity Beans

The EJB specification supports both transient and persistent objects. A transient object is referred to as a session bean, and a persistent object is known as an entity bean.

A Session Bean

A session bean is an EJB that is created by a client and usually exists only for the duration of a single client-server session. A session bean usually performs operations such as calculations or database access on behalf of the client. While a session bean may be transactional, it is not recoverable if a system crash occurs. Session bean objects can be stateless or they can maintain a conversational state across methods and transactions. If a session bean maintains a state, the EJB container manages this state if the object must be removed from memory. However, persistent data must be managed by the session bean object itself.

The tools for a container generate additional classes for a session bean at deployment time. These tools obtain information from the EJB architecture by examining its classes and interfaces. This information is utilized to generate two classes dynamically that implement the home and remote interfaces of the bean. These classes enable the container to intercede in all client calls on the session bean. The container generates a serializable Handle class as well, which provides a way to identify a session bean instance within a specific lifecycle. These classes can be implemented to perform customized operations and functionality when mixed in with container-specific code.

In addition to these custom classes, each container provides a class that provides metadata to the client and implements the SessionContext interface. This provides access to information about the environment in which a bean is invoked.

An Entity Bean

An entity bean is an object representation of persistent data maintained in a permanent data store such as a database. A primary key identifies each instance of an entity bean. Entity beans are transactional and are recoverable in the event of a system crash.

Entity beans are representations of explicit data or collections of data, such as a row in a relational database. Entity bean methods provide procedures for acting on the data representation of the bean. An entity bean is persistent and survives as long as its data remains in the database.

Scenario & Solution

You need to maintain non-enterprise data across method invocations for the duration of a session. What kind of EJB would you use?

You should use a session bean, an EJB that is created by a client and usually exists only for the duration of a single client-server session.

You need to create an EJB to represent enterprise data. What kind of EJB should you use?

You should use an entity bean, which is an object representation of persistent data maintained in a permanent data store such as a database.

An entity bean can be created in two ways: by direct action of the client in which a create() method is called on the bean’s home interface, or by some other action that adds data to the database that the bean type represents. In fact, in an environment with legacy data, entity objects may exist before an EJB is even deployed.

An entity bean can implement either bean-managed or container-managed persistence. In the case of bean-managed persistence, the implementer of an entity bean stores and retrieves the information managed by the bean through direct database calls. The bean may utilize either Java Database Connectivity (JDBC) or SQL-Java (SQLJ) for this method. (Session beans may also access the data they manage using JDBC or SQLJ.) A disadvantage to this approach is that it makes it more difficult to adapt bean-managed persistence to alternative data sources.

In the case of container-managed persistence, the container provider may implement access to the database using standard APIs. The container provider can offer tools to map instance variables of an entity bean to calls to an underlying database. This approach makes it easier to use entity beans with different databases.

Above are some possible scenario questions that will help you review the differences between session and entity beans.


EJB Home | All EJB Tutorials | Latest EJB Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Session Bean = client data is maintained upto predefined session time/period.


Entity Bean = client data is maintained in a persistant db.


View Tutorial          By: Chetankumar at 2012-05-19 11:12:17

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Paul Allen and Joseph Bambara
Managing Security in EJB
Difference Between Stateful and Stateless Session Beans
Difference Between Session and Entity Beans
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component

More Tutorials in EJB
EJB 3 Entities
Web services basics
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component
Difference Between Session and Entity Beans
Difference Between Stateful and Stateless Session Beans
Managing Security in EJB
EJB Entity Beans
Types of EJB

More Latest News
Most Viewed Articles (in EJB )
Difference Between Stateful and Stateless Session Beans
Difference Between Session and Entity Beans
Types of EJB
EJB Entity Beans
Managing Security in EJB
Web services basics
EJB 3 Entities
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component
Most Emailed Articles (in EJB)
Difference Between Session and Entity Beans
Difference Between Stateful and Stateless Session Beans
Managing Security in EJB
Web services basics
EJB 3 Entities
Types of EJB
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component
EJB Entity Beans