What are the methods of Entity Bean?What is the difference between Container-Managed
Persistent (CMP)
bean and Bean-Managed
Persistent(BMP) ?
Container-managed persistence beans are the simplest for the bean developer to create
and the most difficult for the EJB server to support. This is because all the
logic for synchronizing the bean’s state with the database is handled
automatically by the container. This means that the bean developer doesn’t need
to write any data access logic, while the EJB server is supposed to take care
of all the persistence needs automatically. With CMP, the container manages the
persistence of the entity bean. A CMP bean developer doesn’t need to worry
about JDBC code and transactions, because the Container performs database calls
and transaction management instead of the programmer. Vendor tools are used to
map the entity fields to the database and absolutely no database access code is
written in the bean class. All table mapping is specified in the deployment
descriptor. Otherwise, a BMP bean developer takes the load of linking an
application and a database on his shoulders.
The bean-managed persistence (BMP) enterprise bean manages synchronizing its
state with the database as directed by the container. The bean uses a database
API to read and write its fields to the database, but the container tells it
when to do each synchronization operation and manages the transactions for the
bean automatically. Bean-managed persistence gives the bean developer the
flexibility to perform persistence operations that are too complicated for the
container or to use a data source that is not supported by the container.BMP
beans are not 100% database-independent, because they may contain
database-specific code, but CMP beans are unable to perform complicated DML
(data manipulation language) statements. EJB 2.0 specification introduced some
new ways of querying database (by using the EJB QL - query language).
What are the methods of Entity Bean?
An entity bean consists of 4 groups of methods:
1. create methods: To create a new instance of a CMP entity bean, and therefore
insert data into the database, the create() method on the bean’s home interface
must be invoked. They look like this: EntityBeanClass
ejbCreateXXX(parameters), where EntityBeanClass
is an Entity Bean you are trying to instantiate, ejbCreateXXX(parameters)
methods are used for creating Entity Bean instances according to the parameters
specified and to some programmer-defined conditions.
A bean’s home interface may declare zero or more create()
methods, each of which must have corresponding ejbCreate()
and ejbPostCreate() methods in the bean class. These
creation methods are linked at run time, so that when a create()
method is invoked on the home interface, the container delegates the invocation
to the corresponding ejbCreate() and ejbPostCreate() methods on the bean class.
2. finder methods: The methods in the home interface
that begin with “find” are called the find methods. These are used to query the
EJB server for specific entity beans, based on the name of the method and
arguments passed. Unfortunately, there is no standard query language defined
for find methods, so each vendor will implement the find method differently. In
CMP entity beans, the find methods are not implemented with matching methods in
the bean class; containers implement them when the bean is deployed in a vendor
specific manner. The deployer will use vendor
specific tools to tell the container how a particular find method should
behave. Some vendors will use object-relational mapping tools to define the
behavior of a find method while others will simply require the deployer to enter the appropriate SQL command.
There are two basic kinds of find methods: single-entity and multi-entity.
Single-entity find methods return a remote reference to the one specific entity
bean that matches the find request. If no entity beans are found, the method
throws an ObjectNotFoundException . Every entity bean must define the single-entity find
method with the method name findByPrimaryKey(), which takes the bean’s primary key type as an argument.
The multi-entity find methods return a collection (
Enumeration or Collection type) of entities that match the find request.
If no entities are found, the multi-entity find returns an empty collection.
3. remove methods: These methods (you may have up to 2
remove methods, or don’t have them at all) allow the client to physically
remove Entity beans by specifying either Handle or a Primary Key for the Entity
Bean.
4. home methods: These methods are designed and
implemented by a developer, and EJB specification doesn’t have any requirements
for them except the need to throw a RemoteException
is each home method
When should I adopt BMP and when
I should use CMP?
You can use CMP and BMP beans in the same application… obviously, a bean can be
BMP or CMP, not both at the same time (they are mutually exclusive).
There is a common approach that is normally used and considered a good one. You
should start developing CMP beans, unless you require some kind of special
bean, like multi-tables, that cannot be completely realized with a single bean.
Then, when you realize that you need something more or that you would prefer
handling the persistence (performanbce issue are the
most common reason), you can change the bean from a CMP to a BMP.
Static variables in EJB should
not be relied
upon as they may break in clusters.Why?
Static variables are only ok if they are final. If they are not final, they will
break the cluster. What that means is that if you cluster your application
server (spread it across several machines) each part of the cluster will run in
its own JVM.
Say a method on the EJB is invoked on cluster 1 (we will have two clusters - 1
and 2) that causes value of the static variable to be increased to 101. On the
subsequent call to the same EJB from the same client, a cluster 2 may be
invoked to handle the request. A value of the static variable in cluster 2 is
still 100 because it was not increased yet and therefore your application
ceases to be consistent. Therefore, static non-final variables are strongly
discouraged in EJBs.
Can I develop an Entity Bean without implementing the create() method in the home interface?
As per the specifications, there can be ‘ZERO’ or ‘MORE’ create()
methods defined in an Entity Bean. In cases where create() method is not
provided, the only way to access the bean is by knowing its primary key, and by
acquiring a handle to it by using its corresponding finder method. In those
cases, you can create an instance of a bean based on the data present in the
table. All one needs to know is the primary key of that table. i.e. a set a columns that uniquely identify a single row in
that table. Once this is known, one can use the ‘getPrimaryKey()’ to get a remote reference to that
bean, which can further be used to invoke business methods.
What is the need of Remote and Home interface. Why cant it be in one?
The main reason is because there is a clear division of roles and
responsibilities between the two interfaces. The home interface is your way to
communicate with the container, that is who is
responsible of creating, locating even removing one or more beans. The remote
interface is your link to the bean, that will allow
you to remotely access to all its methods and members. As you can see there are
two distinct elements (the container and the beans) and you need two different
interfaces for accessing to both of them.
What is bean managed transaction?
If a developer doesn’t want a Container to manage transactions, it’s possible
to implement all database operations manually by writing the appropriate JDBC
code. This often leads to productivity increase, but it makes an Entity Bean
incompatible with some databases and it enlarges the amount of code to be
written. All transaction management is explicitly performed by a developer.
No comments:
Post a Comment