Entity Bean
An entity bean
directly represents data stored in persistent storage, such as a database. It
maps to a row or rows within one or more tables in a relational database, or to
an entity object in an object oriented database. It can also map to one or more
rows across multiple tables. In a database, a primary key uniquely identifies a
row in a table. Similarly, a primary key identifies a specific entity bean
instance. Each column in the relational database table maps to an instance
variable in the entity bean . Because an entity bean usually represents data
stored in a database, it lives as long as the data.
Regardless of
how long an entity bean remains inactive, the container doesn't remove it from
persistent storage. The only way to remove() method, which removes the
underlying data from the database. Or an existing enterprise application can
remove data from the database.
Persistence
and entity beans
All entity
enterprise beans are persistent; that is ,
their state is stored between sessions and clients. As a bean provider ,
you can choose how your entity bean's persistence is implemented. You can
implement the bean's persistence directly in the entity bean class, making the
bean itself responsible for maintaining its persistence. This is called bean
managed persistence. Or you can delegates the handling of the entity bean's
persistence to the EJB container. This is called container-managed persistence.
Bean
managed persistence
An entity bean
with bean managed persistence contains the code to access and update a
database. That is , you , as the been provider, write database access calls
directly in the entity bean or its associated classes. Usually you write these
calls using JDBC. The database access calls can appear in the entity bean's
business methods, or in one of the entity bean interface methods. (you will
read more about the entity bean interface soon).
Usually a bean
with bean managed persistence is more difficult to write because you write the
additional data access code. And , because you might choose to embed the data
access code in the bean's methods, it can also be more difficult to adapt the
entity bean to different databases or to a different schema.
Container
managed persistence
You don't have
to write code that accesses and updates databases for entity beans with
container managed persistence . Instead, the bean relies on the container to
access and update the database. Container managed persistence has many advantages
compared to bean managed persistence:
v Such beans are easier to code.
v Persistence details can be changed without
modifying and recompiling the entity bean. Instead the deployer or application
assembler can modify the deployment descriptor.
v The complexity of the code is reduced, as
is the possibility of errors. You , as the bean provider, can focus on the
business logic of the bean and not on the underlying system issues.
v Container managed persistence has some
limitation, however. For example , the container might load the entire state of
the entity object into the bean instance's fields before it calls the ejbLoad()
method. This could lead to performance problems if the bean has many fields.
Primary
keys in entity beans
Each entity
bean instance must have a primary key is a value (or combination of values)
that uniquely identifies the instance. For example , a database table that
contains employee records might use the employee's social security number for
its primary key.
The entity bean
modeling this employee table would also use the social security number for its
primary key. For enterprise beans , the primary key is represented by a String
or Integer type or a Java class is a legal value type in RMI_IIOP. This means
the class must extend the java.io.Serializable interface, and it must implement
the Object.equals (Other other) and Object.hasCode() methods, which all Java
classes inherit.
The primary key
class can be specific to a particular entity bean class. That is , each entity
bean can define its own primary key class. Or multiple entity beans can share
the same primary key class.
Writing
the entity bean class
To create an
entity bean class,
Ø Create a class that implements the javax.ejb.EntityBean
interface.
Ø Implement one or more ejbCreate() methods .
If you have already created the home or local home interface for the bean, the
bean must have an ejbCreate() method with the same signature for each create()
method in that interface . If an ejbCreate() method has a parameter, you must
also declare and implement an ejbPostCreate() method.
Ø Define and implement the business methods you
want your bean to have. If you've already created the remote or local
interface.
Ø For entity beans with bean managed
persistence, implement finder methods.
Writing
the business methods
within your
enterprise bean class, write full implementations of the business methods your
bean needs. To make these methods available to a client, you must also declare them
in the bean's local or remote interface using the exact same signature.

No comments:
Post a Comment