EJB 3.0 Features and Advantages

Have you ever identified what are the features and advantages ok EJB 3.0.Ok,here you can get the features and advantages of EJB 3 over EJB 2.0.

When do you really need EJB 3.0 ? What is benefit of EJB 3.0 over EJB 2.0?

EJB 3.0 is included in the next J2EE specification, JAVA EE 5. ( http://java.sun.com/javaee/5/ [http://java.sun.com/javaee/5/] )
The EJB 3.0 specification is defined in JSR 220, which can be found at the following location: http://www.jcp.org/en/jsr/detail?id=220
The publication is published as three separate files:

  • The core
  • The persistence provider
  • The simplified specification, which contains new features

Main advantages of EJB 3.0

The Advantage of EJB3 over EJB 2.x was, EJB 2.x too complex. Developers were using additional tools to make it easier.
• XDoclet (Attribute oriented programming): http://xdoclet.sourceforge.net
• Hibernate for persistence: http://www.hibernate.org
The main focus for this specification is on Ease Of Development (EoD). One major way this has been simplified is by using metadata attribute annotations suppported by JDK 5.0. Simplifying EJB development should produce a wider range of Java EE developers.

The difference with EJB 2.0

  • The deployment descriptors are no longer required; everything can be accomplished using metadata annotations.
  • The CMP (Container Managed Persistence) has been simplified; it is now more like Hibernate or JDO.
  • Programmatic defaults have been incorporated. For example, the transaction model is set to REQUIRED by default. The value needs to be set only if a specific value other than the default value is desired.
  • The use of checked exceptions is reduced; the RemoteException is no longer mandatory on each remote business method.
  • Inheritance is now allowed; therefore, beans can extend some of the base code.
  • The native SQL queries are supported as an EJB-QL (Query Language) enhancements.

New features in EJB 3.0

Metadata Annotations

Metadata annotations is new. For example, to define a stateless session bean, the @Stateless annotation is declared on the bean class.
Business Interceptors – The new business interceptors allow the developer to intercept each business method of the bean.The parameters and the returned values can be changed. For example, an interceptor can be used to determine the time that a method takes to execute.

Lifecycle Interceptors

In addition to business interceptors, the EJB2 callbacks ( such as the ejbActivate() method) are now defined using annotation. For the ejbActivate() method, this is done with the help of @PostActivate annotation. This annotation is set on a method that will be called by the container.

Dependency Injection

Dependency injection makes it possible to request that the container inject resources, instead of trying to get them. For example, with the EJB2 specification, in order to get an EJB, the following code was used:

&
try {
Object o = new InitialContext().lookup("java:comp/env/ejb/MyEJB");
myBean = PortableRemoteObject.narrow(o, MyInterface.clas);
} catch (NamingException e) {
....
}

With EJB3 this is done using only the following code:
@EJB private MyInterface myBean;
If the @EJB annotation is found in the class, the container will look up and inject an instance of the bean in the myBean variable.

Persistence

New features are linked to the persistence layer. For example, EJB3 entities are POJO (Plain Old Java Object). This means that they can be created by using the new() constructor: new MyEntity(); Also entities are managed by an EntityManager: entitymanager.persist(entity); In addition, entities have call backs available.