Writing a CMP entity bean: make it abstract

image with no caption

Make your CMP entity bean class abstract.

You still have to implement the container callbacks from javax.ejb.EntityBean, and if you still have to write all your business methods (including those from the home), and if you have any create methods, you have to match those in the bean class as well. But that still leaves a pile of work for the Container to do, including the accessor methods for your persistent fields. For example, if your Customer table has a column for address, and you map that to a field in your bean class (so that clients can, for instance, call getAddress()), you don’t write the getters and setters for that field. In fact, you don’t even declare the field! With CMP, you create a virtual field, by defining getters and setters. In other words, the persistent field for address exists in your bean simply because there’s an abstract getter and setter for it. (Well... that’s not entirely true. There also has to be an entry in the deployment descriptor, but we’ll look at that in the next chapter).

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.