Beans come in three flavors

Entity

image with no caption
image with no caption

Use an entity bean to represent a thing in a persistent store. That almost always means something in a database, where an instance of an entity bean represents a row in a table (although if the database is normalized, the bean might be drawing from rows in multiple tables). A typical entity example is Customer, where one entity might represent Bo Olean (ID# 343) and another entity might represent Trixia Lin (ID# 870).

Message-driven

Use a message-driven bean only when you need a JMS consumer. In other words, a bean that can listen for messages from a JMS messaging service. Clients never call message-driven beans directly; in order to get a message-driven bean to do something, a client must send a message to a messaging service. That means a message-driven bean has no EJBObject because the server gets the client requests directly from a messaging service rather than as a call from the client to the bean. A typical message-driven bean might be a NewCustomerNotification subscriber.

image with no caption
image with no caption

Session

Use a session bean for... everything else. Almost any kind of back-end service can (and often should) begin with a session bean. Where an entity bean represents a thing, a session bean typically represents a process. To put it another way, when you think of entity beans, think noun, and when you think of session beans, think verb. A shopping session is a typical example of a session bean, while a credit card processing system might be another session bean.

image with no caption
image with no caption

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.