Writing an Entity Bean
Entity Beans: Overview...
- Entities
- The “things” that a business process manipulates
- E.g. Customers, Accounts, Orders, Flights
- The data they contain is stored in database tables
- Entity Bean
- A bean representing a business entity
- The object representation of a row of data in a table
- Entity beans differ from session beans:
- Entity beans are always stateful
- They are always persistent - they map to a database
- They are always shared
- Client can find existing ones as well as create new ones
- Entity beans require a primary key
...Entity Beans: Overview
- Entity beans are shared
- Container creates a pool
- Instances in use represent distinct entities
Entity Beans & Persistence...
- Persistence can be managed by:
- The container
- The bean itself
- Container-managed persistence (CMP):
- At deployment, bean's data is mapped to database
- Container reads / writes data from / to database as needed
- Container can use JDBC or any other database API
- Bean need not contain any persistence logic
- How does the container know when to read/write?
- Tied to transactions (covered later…)
...Entity Beans & Persistence
- Bean-managed persistence (BMP):
- Bean contains logic for reading / writing data
- Container calls life-cycle methods to trigger the logic
- Why manage persistence explicitly?
- To use non-JDBC databases
- Flat files, OO databases, etc.
- To use features not available otherwise
- How we will proceed:
- Container-managed persistence is covered here
- Bean-managed persistence is covered later...
Entity Bean Life Cycle...
- Entity bean life cycle tasks represent SQL operations:
- “Creating” an entity:
- Represents creating a new row in a table
- Container executes an insert statement
- Triggered by calling Home#create( * )
- “Removing” an entity:
- Represents deleting a row from a table
- Container executes a delete statement
- Triggered by calling EJBObject#remove( )
- “Finding” an entity:
- Container executes a select statement
- Triggered by calling Home#find*( )
...Entity Bean Life Cycle
- State diagram for an entity bean. See notes for details...
Writing an Entity Bean...
- You must write four key parts of an entity bean:
- Some are similar/identical to those of a session bean
- The remote interface:
- Describes bean's
public functions
- Identical to session bean
- The primary key:
- Defines key for
getting an instance
- See diagram on next page...
...Writing an Entity Bean
The Remote Interface...
- As with session bean:
- Defines interface for your entity bean's business methods
- Must be public
- Must extend javax.ejb.EJBObject
- Must satisfy requirements of RMI remote interfaces
- What's in the interface?
- Methods representing bean properties
- Entity-related process methods
- Methods for bean properties:
- Entity beans have data, thus have "properties"
- Do not have to map to database fields
- JavaBeans defines get/set patterns
- EJB does not require this but it is still useful
...The Remote Interface
- Entity-related process methods:
- Represent activities performed on bean
- E.g. buy( ) and sell( ) on a Portfolio
- See examples in notes...
- A Customer bean for the online bookstore
- Has only property methods
- A Portfolio bean for the online brokerage
- Has property and process methods
Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
The Primary Key
- Primary Key
- Unique identifier to an entity
- Unique identifier to a row in a database table
- May be system generated or endemic
- May be single or compound value
- For example:
- Customer: system-generated customer ID
- Employee: Social-Security #
- LineItem in an Order: Order # + product #
Primary Key Class...
- Entity beans require a primary key class
- Container uses PK instance to identify bean instance
- In CMP, container creates PK, uses to handle persistence
- Deployment descriptor names the class
- Tells container what to instantiate
- Requirements for a primary key class:
- Class is public
- Implements java.io.Serializable
- Has a public no-args constructor
- Implicit constructor is fine
- Has sensible equals( ) & hashCode( )
methods
- For single-field, non-primitive key:
- Choose the class type of the key field
...Primary Key Class
- For multi-field or primitive keys:
- Write a custom class
- Define a public field for each key component
- PK field names must match bean class field names
- Optional features (for BMP or client use):
- Overloaded constructor
- toString( )
- See notes for examples…
- Book: Single-field, non-primitive
- Customer: Single-field, primitive
- Order LineItem: Multi-field
Continuation of Notes...
- This is a full page of notes.
- The slide is hidden.
...Continuation of Notes...
- This is a full page of notes.
- The slide is hidden.
The Home Interface
- As with session beans:
- Every entity bean has a home interface
- Client uses home to get bean instances
- All clients share one home object for given bean type
- The home interface must extend javax.ejb.EJBHome
- An entity home lets a client:
- Create new entities
- Find existing entities
- An entity home will have:
- create( ) methods
- A findByPrimaryKey( ) method
- Details follow...
create(*)
- “Creating” an entity bean:
- Represents creating a new row in a table
- Container executes an insert statement
- Triggered by calling Home#create( … )
- The create( ) methods:
- The client uses them to create new entities
- Can be overloaded
- Most entity homes will have at least one
- Occasionally a home should have none
- See notes for two examples...
- CustomerHome has two create( ) methods
- BookHome has none
Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
findByPrimaryKey( )
- findByPrimaryKey( )
- Described explicitly in EJB spec
- Looks up entity by primary key, returns remote bean type
- FinderException
- Finder throws javax.ejb.FinderException:
- Defines all failures to find request objects
- Home can have other find*( ) methods
- To locate single entities or sets
- Not covered in this course...
Finders in Use
- An example of finder methods in use:
- An updated ShoppingCartBean class
- To assign a customer:
- ejbCreate( ) uses findByPrimaryKey( ) to locate customer
- To add a book:
- addSelection( ) uses findByPrimaryKey( ) to locate book
- Throws FinderException if failed