Session Beans
Encyclopedia
In the Java Platform, Enterprise Edition
specifications, a Session Bean is a type of Enterprise Bean
. The only other type is the Message-driven bean. Legacy EJB versions from before 2006 (EJB3) had a third type of bean, the Entity Bean
. In EJB 3.0 (Java EE 5) those Entity Beans have been replaced by Java Persistence API
entities.
Contrary to JPA Entities, which represent persistent data maintained in a database, a Session Bean implements a business task and is hosted by an EJB container.
A session bean performs operations, such as calculations or database access, for the client. Although a session bean can be transactional, it is not recoverable should a system crash occur. Session bean objects either can be stateless or can maintain conversational state across methods and transactions. If a session bean maintains state, then the EJB container manages this state if the object must be removed from memory. However, the session bean object itself must manage its own persistent data.
Local Stateless SessionBean Hello World
example:
Remote Stateless SessionBean Hello World
example:
Home interface, declares create, destroy and finder methods for the EJB depending on type:
The implementing EJB class:
A simple client:
Java Platform, Enterprise Edition
Java Platform, Enterprise Edition or Java EE is widely used platform for server programming in the Java programming language. The Java platform differs from the Java Standard Edition Platform in that it adds libraries which provide functionality to deploy fault-tolerant, distributed, multi-tier...
specifications, a Session Bean is a type of Enterprise Bean
Enterprise JavaBean
Enterprise JavaBeans is a managed, server-side component architecture for modular construction of enterprise applications.The EJB specification is one of several Java APIs in the Java EE specification. EJB is a server-side model that encapsulates the business logic of an application...
. The only other type is the Message-driven bean. Legacy EJB versions from before 2006 (EJB3) had a third type of bean, the Entity Bean
Entity Bean
An Entity Bean is a type of Enterprise JavaBean, a server-side J2EE component, that represents persistent data maintained in a database. An entity bean can manage its own persistence or can delegate this function to its EJB Container . An entity bean is identified by a primary key...
. In EJB 3.0 (Java EE 5) those Entity Beans have been replaced by Java Persistence API
Java Persistence API
The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework managing relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition....
entities.
Contrary to JPA Entities, which represent persistent data maintained in a database, a Session Bean implements a business task and is hosted by an EJB container.
A session bean performs operations, such as calculations or database access, for the client. Although a session bean can be transactional, it is not recoverable should a system crash occur. Session bean objects either can be stateless or can maintain conversational state across methods and transactions. If a session bean maintains state, then the EJB container manages this state if the object must be removed from memory. However, the session bean object itself must manage its own persistent data.
Stateless Session Beans
A stateless session bean is an object that does not have an associated conversational state, but may have instance state. It does not allow concurrent access to the bean. The contents of instance variables are not guaranteed to be preserved across method calls. All instances of a stateless session bean should be considered identical by the client.Local Stateless SessionBean Hello World
Hello world program
A "Hello world" program is a computer program that outputs "Hello world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to...
example:
Java EE 6
Remote Stateless SessionBean Hello World
Hello world program
A "Hello world" program is a computer program that outputs "Hello world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to...
example:
Java EE 5
J2EE 1.4
Remote interface, declares methods clients can invoke on the EJB:Home interface, declares create, destroy and finder methods for the EJB depending on type:
The implementing EJB class:
A simple client: