EJB 3.0 in a nutshell

An overview and critical analysis of the latest EJB specification

1 2 Page 2
Page 2 of 2
  • Support for bulk update and delete.
  • Explicit support for both inner and outer JOIN operations. A FETCH JOIN allows you to indicate that the related entities—e.g., Orders for a Customer—should also be fetched, though the SELECT clause only selects Customers.
  • More than one value can be returned from the SELECT clause. In fact, you can return a dependent class like this:

          SELECT new CustomerDetails(c.id, c.status, o.count)
          FROM Customer c JOIN c.orders o
          WHERE o.count > 100
    
  • Support for GROUP BY and HAVING.
  • Support for nested subqueries in the WHERE clause.

With the proposed specification, EJB QL is much closer to SQL than ever before. In fact, the specification supports native SQL directly, as noted above. This can prove disturbing for some programmers, and we'll come to that shortly.

Miscellaneous

Method permissions are specified through the @MethodPermissions or @Unchecked annotations; same goes for transaction attributes of methods that can be specified through the @TransactionAttribute annotation.

That still leaves the resource references and resource environment references. These can also be specified through annotations, but in slightly different way. Such context dependencies are handled with the injection facility. The container automatically initializes appropriately annotated instance variables of a bean with external references to environmental resources. For example, you can get a handle to a DataSource by doing the following:

@Resource(name="myDataSource") //Type is inferred from variable
public DataSource customerDB;

If the resource name is not specified, it defaults to the property name (customerDB, in this case). When all reference properties can be inferred, the @Inject annotation may be used instead:

@Inject public DataSource customerDB;

The container is responsible for initializing customerDB with the appropriate DataSource instance at runtime. The deployer must define these resources to the container at deployment time.

Yet more good news: the checked exceptions are gone. Though you can declare any arbitrary application exceptions, you do not have to throw and catch other exceptions like CreateException and FinderException . The container throws system-level exceptions wrapped in javax.ejb.EJBException or just throws IllegalArgumentException or IllegalStateException, where required by the spec.

EJB file-processing models

Before we wind up this section, let's quickly discuss how the container provider's EJB processing model could change. The spec is silent on this issue but I can see at least two models.

One way is to first process an EJB file to generate deployment artifacts (required interfaces and deployment descriptors) closer to the EJB 2.1 deployment model and deploy the EJB component similarly to how EJB 2.1 deploys it. Of course, such generated deployment descriptors and files can be nonstandard, but they might resemble the current ones. This approach, illustrated in the figure below, reduces rework for container providers and particularly reduces the burden of supporting both EJB 2.1 and EJB 3.0-style EJB components.

A potential EJB file-processing model. Click on thumbnail to view full-sized image.

Another possibility is a JSP-like (JavaServer Pages) drag-and-drop deployment model. You could drop an EJB file into a predesignated, implementation-defined directory and the container will pick it up, process it, deploy it, and make it available for use. This model can be built on top of the above approach, but will be a tremendous help in supporting iterative development. Considering that ease of development is one of the prime goals behind the EJB 3.0 specification, I sincerely hope the next draft would specify such a model (at least a nonnormative one).

What do you think?

EJB 3.0 is surely a job well done, and the expert group deserves all the praise for a wonderful effort towards making the life of an EJB programmer easier. With that said, life will be easier, but surely not easy, with more than 50 annotations already defined (several more expected in the next draft), each with its own set of rules for defaults and other behavioral specifications. Of course, I wasn't really expecting an "EJB 3.0 = EJB 2.1 for dummies" kind of equation. Still, I would like to express my humble opinion about a few issues:

  • For one, the specification has surely made iterative development easier with fewer program artifacts, intuitive defaults, and a simpler model for access to the runtime environment. I also like the fact that the home interface is gone, which means fewer artifacts to even visualize.
  • In earlier EJB specifications, entity beans used to be a view into the persistent store. It was theoretically (and probably only theoretically) possible to map an entity bean to any legacy EIS (enterprise information system) also. Doing so was accomplished with longer-term extensibility in mind and was meant to allow wider adoption of entity beans for business data modeling. Instead, the accompanying complexity only hurt the prospects of entity beans. As proposed in this draft, an entity bean is just a view into the database. Life is much simpler with no abstract persistent schemas and a simpler data-access model.
  • While I warmly welcome the above change, I'm not comfortable with the idea of having SQL snippets in EJB code (annotations). Some developers are totally against even the apparent "SQLness" of annotations (like @Table and @Column annotations). In my opinion, this "SQLness" is okay—after all, we are clear that we are talking about databases. But SQL snippets like columnDefinition="BLOB NOT NULL" make me really nervous because such an approach encourages a tight coupling between EJB code/annotations and SQL representation of the same.
  • Though support for native SQL queries appears tempting, embedding SQL in EJB code is a very, very bad idea. Of course there are ways to avoid hard-coding your SQLs, but those approaches would serve better as part of the specification, rather than individual developers' custom patterns.
  • I assume that use of annotations like @Table is only declarative in the bean class. The table name specified through the name member of the @Table annotation will probably have to be mapped to actual database tables at deployment time. The specification should be explicit on this issue and prescribe a consistent model.
  • The specification also needs to be more explicit about changes to the client-programming model, particularly standalone Java clients. All references to the client-programming model in the draft have implicitly assumed EJB clients. The draft also fails to touch on the issue of backward compatibility for client programs.
  • The Transient annotation should probably be renamed to avoid conflict if it is used in place of the transient keyword. In fact, on this issue, I believe we should be willing to deviate slightly from the configuration-by-exception approach and define an @Persistent annotation to explicitly mark all persistent fields. The @Persistent annotation could be just a marker annotation or it could have a few annotation members to possibly associate with O/R mapping annotations.

Relationship to other specifications

Some of the concurrently developing Java Specification Requests that might affect EJB 3.0 are JSR 175 (Metadata Facility for the Java Programming Language) and JSR 181 (Web Services Metadata for the Java Platform).

JSR 175 is mostly finalized and should not have much of an impact even if changes result; whereas, JSR 181 has two associations with EJB 3.0:

  • Web service interface: The EJB specification will adopt the mechanism laid out by JSR 181 that specifies that a bean implements a Web service and how the Web service is exposed to clients.
  • JSR 181 proposes different mechanisms for specifying security semantics. EJB 3.0's proposals are more in line with the mechanism in earlier EJB specifications (MethodPermissions), but JSR 181 proposes a slightly different approach (SecurityRoles and SecurityIdentity annotations). Similarly, the definition of the RunAs annotation also slightly differs. This issue is still open and will be decided at the J2EE level to maintain a consistent mechanism throughout.

Some other developing specifications targeted at J2EE 1.5 might have some association with EJB 3.0. But, with the exception of a few platform-level annotations for transactions and security discussed above, currently no other developing specs will have an impact on EJB 3.0.

Conclusion

We still have a long way to go before EJB programming becomes a pleasant experience—one that doesn't require exceptional expertise. But the expert group surely has made a good start at reducing complexity. O/R mapping proposals are in their early phases, and the expert group is still working on them. We just hope the model won't turn out to be as complex and excessively SQL-bound as it seems at this stage. Let's not just stop there (wishin 'n' hopin 'n' thinkin 'n' prayin): express your opinion by sending your comments to the expert group at ejb3-feedback@sun.com. The Java Community Process is not exactly a democracy, but your opinion counts.

The views in this article are the author's own and not related to his employer in any way. The author would like to thank Hemant Khandelwal for his help in reviewing this article.

The downloadable source code is a simplified version of illustrative examples provided in the EJB 3.0 early draft. Since no tools are available, the samples are not verified. Use them just as an illustrative sample of what EJB 3.0 beans will look like.

Anil Sharma is a senior engineer working for BEA Systems on the WebLogic Integration product. He has spent most of his career working on J2EE technologies, working for Oracle and Pramati Technologies in product development teams before joining BEA. In his spare time, he likes to listen to music and watch movies; he has also started getting involved with some open source initiatives.

Learn more about this topic

1 2 Page 2
Page 2 of 2