By: Paul Allen and Joseph Bambara Printer Friendly Format
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.
Most Viewed Articles (in EJB )
Latest Articles (in EJB)
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Subscribe to Tutorials
1. Session Bean = client data is maintained upto pred
View Tutorial By: Chetankumar at 2012-05-19 11:12:17
View Tutorial By: http://www.google.com/recaptcha/api/image?c=03AHJ_VutQw3YzsJXjYMBAiolkK127vhZCwAalzNjfs8OP0TGqR0UwuHKJbkKUZVSctjjjuglnqgFL1LZB9A5MXSNACYNWY_jjwoGDsrJEVFI_X-3yAv0He7ph9Jp3vOQRY1uGTbWvCYfdMSs7Vd4pOHa_5U1tqCKnagtWuFYs3Cr8O2K4bmvaRwdsJ7EO8YBqVoQUPsDXyE-pfS7-O at 2015-03-27 13:30:55
View Tutorial By: http://www.google.com/recaptcha/api/image?c=03AHJ_VutkD4asEEuHIwubWidTpaqL22rsPosCUGfisBAhdm11UOtUBXUJuR1hjcIsd68wTvYnKeJOg-R8UZyp_0JkU2bd80EnDxQR0nBndS3EvQRDY8yVElaIrwDHggsGdczLUbBlKHq3uJpjE9xTWootEUsLXhvKcAfu1gZNbLdnBoA_zMQXpnBv3rUfzCFG4cacB9FwS1_OnuFrp at 2015-03-27 13:31:26
View Tutorial By: http://www.google.com/recaptcha/api/image?c=03AHJ_VutkD4asEEuHIwubWidTpaqL22rsPosCUGfisBAhdm11UOtUBXUJuR1hjcIsd68wTvYnKeJOg-R8UZyp_0JkU2bd80EnDxQR0nBndS3EvQRDY8yVElaIrwDHggsGdczLUbBlKHq3uJpjE9xTWootEUsLXhvKcAfu1gZNbLdnBoA_zMQXpnBv3rUfzCFG4cacB9FwS1_OnuFrp at 2015-03-27 13:31:49