Home business methods to the rescue

That’s right... business methods aren’t just for the component interface, when you’re talking about entity beans. As of EJB 2.0, an entity bean home can have methods that—drum roll—don’t have to return component interfaces! Home business methods can return anything (with the one restriction, of course, that Remote home methods return values that are RMI-IIOP compliant).

Home business methods are great for batch operations, or for query methods where the client doesn’t need—or want—EJB object references, but simply wants the entity’s data (in other words, the data for one or more of the entity’s persistent fields). For example, we might put a home business method in the Customer bean like, getAllCustomerInfo(), that returns a collection of Strings, with whatever pieces of data you’ve decided make up the customer’s info. Better yet, you can send back a collection of CustomerInfo objects, where CustomerInfo is a class that simply holds the data (and getters) for the Customer’s persistent state. That way, the client can make local calls to get the data it needs out of the CustomerInfo objects, without having those calls be remote calls on the component interface.

A CustomerInfo class is an example of a Value Object class which is, in a nutshell, just a class with getters (and possibly setters, depending on the design) representing the entity’s persistent fields). And it, too, has a dark side—the data starts to become stale the moment after the Value ...

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.