Entity Beans in EJB(CMP)

Entity beans are characterized by the following 3 features.

  1. They are 'Persistent'. ( they are stored inhard-disk)
  2. They are shared by many clients.
  3. They have ,'Primary key'.

also read:

As already mentioned ,Entity beans can be thought of as a record ( or row) in a table of a relational database. ( This is just for easy understanding because, the database can also be Object Database, XML database etc.)

Let us consider a simple Java class named'customer'. Let this class havejust three attributes ,namely,'key','Name'and 'Place'. In a javabean, we would provide accessor methods, such as 'getName()'&'setName(String s)etc. for each attribute. The same method is employed in Entity bean. ( Roughly).

Thus,we deal with Java idiom only andthis is more intuitive.If we have an account bean, we can write code for deposit as follows:




Doubtless, this is much simplerand easier than writing sql code.

Entity beans 'persist' (ie) they are stored inEnterprise server's hard disk and so even if there is a shutdown of the server, the beans 'survive' and can be created againfrom the hard disk storage.[A session bean is not stored in hard disk].

A Session bean , whether it isstateless or statefulis meant for a single client. But Entity bean , being a record in a table of a database, is likely to be accessed by a number of clients . So, they are typically shared by a number of clients. For the same reason, entity beans should work within 'Transaction'.managementas specified. in the Deployment descriptor.

Canwe use a session beanfor accessing a database either for 'select' query or for modifying?

Yes. We can, provided that the application is simple &is not accessed by multiple users . But we should note that the session
beanjust displays or manipulates a record in database whereas the entity bean is the record itself!

But, typically in an enterprise situation, a database will be accessed by thousands of clients concurrently, and the very rationale for the development of EJB is to tackle the problems which arise then. That is why, Entity beans are the correct choice for Enterprise

If we think of an entity bean instance as a record in a table of database, it automatically followsthat itshouldhave a primary key for unique identification of the record..[Many books provide a 'primary key
class'.But it is not atall necessary.] But carefully note that it should be a serializable java class. So, if we provide a primary key as 'int' type, we will have to provide a wrapper class (ie) Integer. This is clumsy. The best and easiest method
is to provide a string type as the primary key. (String class). This is the method that we will be following in our illustarations.
( We are using WebLogic 5.1)
So, in our example, we are having an Accessdatabase named 'customer'.This database has a table known as 'table1'. The table has three columns .

  1. 'key'(primary key field)
  2. 'name'
  3. 'place'

( all of them are ofStringtype)

We create a table like this without any entries and then register it in ODBC.( this is the most familiar and easy approach.We can also use other types of jdbc drivers.)

[This does not mean that the recordshaveto be created only through Entity bean. We can always add, delete and edit records directly in the table].

Entity beans can have two types of Persistence.

  1. Container-managed Persistence(CMP)
  2. Bean-managed Persistence type(BMP)

We can declare the type of persistence required by us in the 'Deployment Descriptor'.

In CMP, the bean designer does not have to write any sql-related code at all. The necessary sql statements are automatically generated by the container. The container takes care of synchronizing the entity bean's attributes with the corresponding columns in the table of the database. Such variables are referred to as 'container-managed fields'.

This requirement also is declared by us in the Deployment descriptor.

With CMP, the entity bean class does not contain the code that connects to a database. So, we are able to get flexibility by simply editing the deployment descriptors and weblogic.properties files, without editing and recompiling the java class files.

What are the advantages of CMP?

CMPbeanshave two advantages:

  1. less code.
  2. the code is independent of the type of data store such as Relational database.

What are the limitations of CMP?

If we want to have complex joins between different tables, CMP is not suitable. In such cases, we should use BMP.


As before we begin with the Remote Interface file.


import javax.ejb.*;

import java.rmi.*;

public interface customerRemote extends EJBObject

public StringgetName()throws

public voidsetName(Strings) 

throws RemoteException;

public StringgetPlace()throws RemoteException;

publicvoidsetPlace(String s)

throws RemoteException;


Next we write the home interfcae.

//**********customerHome.java *******************

import javax.ejb.*;

import java.rmi.*;

public interface  customerHomeextends EJBHome


public customerRemote create

(String a,String b,String c)

throwsRemoteException, CreateException;

public customerRemote findByPrimaryKey(String a) 

 throwsRemoteException, FinderException;



import javax.ejb.*;
import java.rmi.*;

public class customerBean implements EntityBean

public Stringkey;
public Stringname;

public Stringplace;

public StringgetName()









public void setName(String b)




publicvoidsetPlace(String c)





public StringejbCreate

(Stringa, Stringb, Stringc) 

throws CreateException



this.place = c;
return null;//it should be null!

public void  ejbPostCreate

(String a,String b,String c)

throws CreateException{}

public void ejbActivate(){}


public void ejbRemove(){}


public void ejbStore(){}

public void setEntityContext(EntityContext
ec){ }

public void unsetEntityContext(){ }

We now createthreexml files as given below.

  1. ejb-jar. xml
  2. weblogic-ejb-jar.xml
  3. weblogic-cmp-rdbms-jar.xml

These three files are very important and should be created with utmost care. Remember that XML is case-sensitive and the DTD (Deployment descriptor) for each file expects the correct structure of the document. So type exactly as given.(No formatting..shown here for clarity only)

ejb-jar. xml

ejb-jar PUBLIC
Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN'


version="1.0" ?>
weblogic-ejb-jar PUBLIC
Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>