YOUR FEEDBACK
EJM wrote: Well versed article and nice explanation. Easy to understand especially for us w...


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


The Simplicity of EJB 3.0
A step in the right direction

Over the past few years, the Enterprise JavaBeans (EJB) specification has evolved significantly. In the early days of EJB, application developers faced a burden of overwhelming complexity: they had to manage several component interfaces, deployment descriptors, and unnecessary callback methods; work within the limitations of the EJB Query Language (EJBQL); and learn and implement the design patterns used to overcome the limitations of the specification.

The introduction of the EJB 2.1 specification did improve things, although many still say the specification is too complex - and that criticism is often seen as a reflection of the problems of the entire J2EE platform.

The next major release of the J2EE 5 platform is focused on ease of development. As a cornerstone of the platform, much of the effort centers on reducing the complexity of EJB. The EJB 3.0 specification simplifies development by removing the requirements for interfaces, deployment descriptors, and callback methods and by adopting regular Java classes and business interfaces as EJBs.

The specification also leverages metadata annotations that are standardized with JSR-175, and the proven Plain Old Java Object (POJO) persistence architecture used by object-relational (O/R) frameworks such as Oracle TopLink and Hibernate. These last two features have greatly reduced much of the specification's complexity. Now you can take a regular Java class, add annotations to it, and deploy it to an EJB 3.0 container as an entity. A configuration by exception approach is taken so that the container accepts the defaults whenever possible.

Sample Entity Bean with Annotations

@Entity
@Table(name="PLAYER", schema="CMPROSTER")
@NamedQuery(name="findAll",queryString="SELECT OBJECT(p) FROM Player p");
public class Player implements Serializable
{
  @Id
   @Column(name = "ID", primaryKey = true, nullable = false)
   public String getId()
   {
    return id;
   }
//………………..
}

The features I've mentioned above are only the tip of the iceberg - the EJB 3.0 specification provides a slew of new features and enhancements.

This all sounds great on paper, but I wanted to find out just how much easier it is to develop applications with EJB 3.0. So, I decided to give the specification a spin and see for myself. I chose an existing EJB 2.1 application that implements some common use cases with design patterns such as a Session façade, and migrated the application using the new features of EJB 3.0. I used the publicly available demo application RosterApp (included with the J2EE 1.4 tutorials), which lets you maintain team rosters for players in leagues.

I took the bottom-up approach to migrate RosterApp with EJB 3.0 technology, starting with:

  • Entity beans
  • Data transfer objects (DTO)
  • Session bean
  • Utility and client classes
Migrating the Entity Beans
RosterApp has three entity beans: LeagueBean, TeamBean, and PlayerBean. Instead of taking the existing beans, deleting the home and local interfaces, and converting the abstract methods to getter and setter methods with annotations, I reverse-engineered the RosterApp tables from an Oracle Database 10g as EJB 3.0 entities. My result was three simple POJOs (League, Player, and Team) with a set of default annotations. All I had to do was add annotations for the many-to-many relationship between Player and Team. The annotations look like Listing 1.

The EJB 3.0 specification lets you specify O-R metadata via annotations. It provides a wide range of annotations that cover different types of relationships between POJOs, constraints, column information, sequence generators, composite primary key, and inheritance.

Once you migrate all the O/R mappings as annotations in the POJOs, the next step is to convert a bunch of finder methods with EJBQL from EJB 2.1 to new POJOs. Most of these finder methods were already defined for the player bean. EJB 3.0 provides the NamedQueries annotation to group together individual NamedQuery objects. I took all the EJBQL from the existing application and created a NamedQueries annotation, which looks like Listing 2.

The EJB 3.0 specification provides a Query API that can be used for both static and dynamic queries. A named query can be defined as a standalone query or attached to a query method of the bean class. You can define named queries in EJBQL or SQL. This is a boon for Java developers familiar with SQL syntax, as they can become EJB developers without having to learn another query language.

Mappings and finders covered almost 90-95% of the entity bean migration. The remaining part of the project consisted of ejbSelect statements and methods that perform add and remove operations on the Team POJO. I needed to simplify these methods. The following code shows one of the methods before and after migration. ejbSelect methods were migrated as NamedQuery in the Session facade (which is discussed later in this article).

// remove operation on Player before migration

public void dropPlayer(Player player)
{
Debug.print("TeamBean dropPlayer");
try {
Collection players = getPlayers();
players.remove(player);
}
catch (Exception ex) {
throw new EJBException(ex.getMessage());
}
}
//remove operation after migration

public void dropPlayer(Player player) {
Debug.print("TeamBean dropPlayer");
getPlayers().remove(player);
}

Migrating DTOs
DTOs are the next layer in RosterApp. The entities in EJB 3.0 are POJOs; you can directly transfer them between the business and client tiers without first having to create a separate set or layer of classes as in EJB 2.1. The existing RosterApp used DTOs to transfer Teams, Players, and Leagues data collections between the client and Session facade. The new EntityManager API in the EJB 3.0 persistence specification, which is used to create, remove, find, and query entities, works nicely to attach and detach objects from the persistence context. The EntityManager's merge operation lets you propagate state from detached entities onto persistent entities managed by the EntityManager.

About Raghu R. Kodali
Raghu R. Kodali is consulting product manager and SOA evangelist for Oracle Application Server. He leads next-generation SOA initiatives and J2EE feature sets for Oracle Application Server, with particular expertise in EJB, J2EE deployment, Web services, and BPEL. He holds a Masters degree in Computer Science and is a frequent speaker at technology conferences. Raghu is also a technical committee member for the OASIS SOA Blueprints specification, and a board member of Web Services SIG in OAUG. He maintains an active blog at Loosely Coupled Corner (www.jroller.com/page/raghukodali).

YOUR FEEDBACK
Kiran wrote: An excellent, no-BS, upto the point article.
LATEST JAVA STORIES & POSTS
Join Scott Guthrie as he discusses Microsoft’s commitment to web standards development, Rich Internet Applications and how Microsoft is contributing to help move the web forward. Join Adobe’s Kevin Lynch as he demonstrates how Flash and HTML come together to make the most eng...
At last year's JavaOne Chris Oliver gave a presentation on JavaFX in which he discussed how he was interested in programming Java2D not in terms of JComponent paintEvent methods that launch into graphics.drawLine(…) or graphics.drawRect(…) code, but instead by allowing the de...
Before describing solutions available for rich client application development, it would be a good idea to explain what exactly a rich client application is and which rich client topologies can feasibly be built using the Java platform. In the main, a rich client is a part of a so...
It's sometimes argued that the Java Community Process's development procedures are secretive and that the general public is excluded from participating. While this may have been the case in the past, it's no longer true. The majority of JCP Expert Groups now do their work in an o...
The one thing that unifies the distributed computing style known as SOA, in most of its manifestations, is self-describing data via the Extensible Markup Language (XML). The benefits of XML over opaque message formats in data interchange are well established. No matter if your fo...
In the past couple of years, interest in Jetty has surged. Jetty is an open source Java-based web and application server and servlet container, but what else do you know about it? To commemorate the 12th anniversary of Jetty, here are 12 things that might surprise you
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

SPONSORED BY INFRAGISTICS
In every field of design one of the first things students do is learn from the work of others. They ...
There are many forces that influence technological evolution. After a decade of building enterprise ...
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
ADS BY GOOGLE