Monday, April 18, 2011

EJB 3 Types

As I mentioned above there are three types of beans
  • Session beans
  • Message-Driven beans
  • Entities
Each bean serves a purpose and can use a specific subset of EJB services, the Container manages session and message-drive beans and the persistence provider manages entities
Session beans are invoked by a client to perform a specific business task (check bank balance), a session bean is available for a "unit of work" and thus will not survive a server crash or shutdown. There are two types of session bean
  • Stateful - automatically saves state information between client invocations without having to write additional code, i.e a shopping cart
  • Stateless - do not maintain state information and model app services that can be completed in a single client invocation i.e check bank balance
Message-Driven beans (MDBs) process business logic, they are different in that clients never invoke MDBs directly. They are trigger by messages sent to a messaging server, which enables sending asynchronous messages between system components. There a number of messaging servers like IBMs WebSphere, Oracles Advanced Queuing, etc. MDBs are used for robust system integration or asynchronous processing.
Before we discuss entities we need to discuss the Java Persistence API (JPA), persistence is the ability to have data contained in Java objects automatically stored in a relational database like Oracle, this is managed by the JPA. It uses a technique called Object-Relational Mapping (ORM) which essentially maps's data held in Java objects to the database tables using configuration, so you do not have to write low-level JDBC code. Since JPA standardizes ORM frameworks for the Java Platform you can plugin ORM products like JBoss Hibernate, Oracle TopLink, etc. The JPA defines a standard for
  • The creation of ORM configuration metadata for mapping entities to relational tables
  • The EntityManager API - a standard API for performing CRUD (create, read, update, delete)/persistence operations for entities
  • The Java Persistence Query Language (JPQL) for searching and retrieving persisted application data.
Entities are Java objects that are persisted into the database, they model the lower-level application concepts that high-level business processes manipulate, basically they are the OO representation of the application data stored in the database, thus they will survive crashes and shutdowns. JPA entities support the following
  • Relationships
  • Inheritance
  • Polymorphism
The EntityManager interface manages entities in terms of actually providing persistence services, the JPA provider tells entities how to map to the database, the entityManager performs the actual persistence operations. The following is a summary of what each can do
  • JPA - handles lifecycle management, performance tuning, caching, transaction management
  • EntityManager - can add, delete, update and retrieve entities from the database.
JPA provides a specialized SQL-like query language called the Java Persistence Query Language (JPQL) to search for entities saved into the database. It also supports native database specific SQL should you ever need it.