Home > Articles > Programming > Java

What's New in EJB 2.0

  • Print
  • + Share This
Mark Wutka gives an overview of the new features of EJB 2.0.
Mark Wutka gives an overview of the new features of EJB 2.0.

Mark is the author of Special Edition Using Java Server Pages and Servlets (2000, Que).

Version 2.0 of the EJB specification addresses several shortcomings of the existing EJB specification and also adds a new kind of Enterprise Java Bean. Although the specification hasn't been finalized, you can already try out the new features using either the latest version of the Orion server or version 6.0 of BEA's WebLogic server. The three main additions to EJB 2.0 are listed here:

  • Ways to define your own methods in the Home interface

  • A query language for defining finder methods

  • A new container-managed persistence model

  • Message-driven beans

Home Methods

One of the problems that you often encounter when using EJB 1.1 is that you can't define an entity bean method that operates on a range of entity beans. You must put the method in a session bean instead. For example, suppose that you create an online shopping application, and you implement the shopping cart as an entity bean. At some point, you want to go through the database and delete all the shopping carts that haven't been used in a long time. To do this under EJB 1.1, you have two choices: Use a finder method to locate all the carts and then delete each one, or create a session bean that can delete the carts.

Both of these choices have drawbacks. First, when you look at the application as a three-tiered architecture with a presentation layer, business logic layer, and data layer, deleting expired carts belongs in the business logic layer—it's one of your normal business processes. If you write a client program that deletes the carts, you're really moving business logic out of the business logic layer. It makes the application harder to maintain.

Putting the logic in a session bean is a reasonable alternative and, under EJB 1.1, is probably the best solution. You still have a problem, though. Do you create a special session bean to handle a particular entity bean's operations, or do you group them all into one session bean? When you group them together, maintenance may be a little more difficult, and your session bean may be performing a wide variety of operations. Although there's nothing technically wrong with this approach, you usually want your EJBs to focus on specific problems; a catchall bean is not very focused.

If you create separate session beans, of course, you end up with a lot of extra beans. After all, you must create three separate files just to implement a single bean. That's probably more of a maintenance headache than sticking all the methods into a single session bean.

The EJB 2.0 solution to this problem is a method called a Home method. You can define your own methods in the Home interface of aan entity bean. Home methods aren't associated with particular bean instance, much like the finder methods. Home methods typically perform operations such as deleting expired shopping carts or reassigning employees from one division to another.

To define a Home method, just add it to your Home interface. The only restriction on the method name is that it can't start with find, create, or remove. For example, the method to expire shopping carts might be declared this way:

public void expireShoppingCarts() throws RemoteException

When you implement a Home method, put ejbHome in front of the method name in your implementation class. For example, the implementation of the expireShoppingCarts method would be declared like this:

public void ejbHomeExpireShoppingCarts()
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.