What does the client really want from an entity bean home?

image with no caption

With session beans, that was easy—a reference to the component interface. And that’s exactly what the create() methods have to give back.

With entity beans, it’s the same for create() methods—they must give back a reference to the component interface, in this case the component interface for the entity just created.

But what if you want to find an existing entity bean instead of making a new one? That’s what the mandatory findByPrimaryKey() method is for, and it, too, must give back a reference to the component interface for the bean matching that key.

But what if there isn’t a matching entity? If there’s no entity with that key in the database, the client gets a javax.ejb.ObjectNotFoundException. So the return type of findByPrimaryKey() is always the same as it is for create(), the component interface for that bean type. (And of course, the rules for session bean client interfaces applies here as well—a Remote home interface must give back the Remote component interface, and the local home interface must give back the local component interface.)

This still leaves us with a method that cannot return the component interface: a multiple-entity finder, like our findByCity() method. Well, the client’s goal doesn’t change with multiple-entity finders; the client still wants a reference to the component interface, only this time it ...

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.