Entity
beans are characterized by the following 3 features.
a)They are 'Persistent'. ( they are stored in
hard-disk)
b)They are shared by many clients.
c)They have , 'Primary key'.
As already mentioned ,Entity beans can
be thought of as a record ( or row) in a table of arelational 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:
intn= account1.getBalance();
n=n+400;
account1.setBalance(n);
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
situations.
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 .
a) 'key'(primary key field)
b) 'name'
c) '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 canhavetwo types of Persistence.
a) Container-managed Persistence(CMP)
b) 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
requirementalso 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:
i)less code.
ii) the code is independent of
the type of
data store such as Relational
database.
What
are thelimitations of CMP?
Ifwe
want to have complex joins between different tables, CMP is not
suitable. In such cases, we should use BMP .
============================================================
EXAMPLEFORCMP -ENTITY BEAN
=================================
As
before we begin with the Remote Interface file.
//customerRemote.java
import
javax.ejb.*;
import
java.rmi.*;
public
interface customerRemote extends EJBObject
{
public StringgetName()throws
RemoteException;
public voidsetName(Strings)
throws
RemoteException;
public StringgetPlace()throws RemoteException;
publicvoidsetPlace(String s)
throws
RemoteException;
}
————————————————————
Nextwe write the home interfcae.
//**********customerHome.java *******************
import
javax.ejb.*;
import
java.rmi.*;
publicinterfacecustomerHomeextendsEJBHome
{
publiccustomerRemotecreate
(String
a,String b,String c)
throwsRemoteException, CreateException;
publiccustomerRemotefindByPrimaryKey(String a)
throwsRemoteException, FinderException;
}
————————————————————
//customerBean.java
import
javax.ejb.*;
import
java.rmi.*;
public
class customerBean implementsEntityBean
{
publicStringkey;
publicStringname;
publicStringplace;
publicStringgetName()
{
returnname;
}
publicStringgetPlace()
{
returnplace;
}
//—————————
publicvoidsetName(String b)
{
name=b;
}
publicvoidsetPlace(String c)
{
place=c;
}
//——————————-
publicStringejbCreate
(Stringa, Stringb,Stringc)
throws
CreateException
{
this.key=a;
this.name=b;
this.place = c;
return null;//it should be null!
}
public void ejbPostCreate
(String
a,String b,String c)
throws
CreateException{}
publicvoidejbActivate(){}
publicvoidejbPassivate(){}
publicvoidejbRemove(){}
publicvoidejbLoad(){}
publicvoidejbStore(){}
public voidsetEntityContext(EntityContext
ec){ }
public voidunsetEntityContext(){ }
}
————————————————————
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
=========
<?xml
version="1.0"?>
<!DOCTYPE
ejb-jar PUBLIC
'-//Sun
Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN'
'http://java.sun.com/dtd/ejb-jar_1_1.dtd'>
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>customerBean</ejb-name>
<home>customerHome</home>
<remote>customerRemote</remote>
<ejb-class>customerBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-field>
<field-name>key</field-name>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
</cmp-field>
<cmp-field>
<field-name>place</field-name>
</cmp-field>
<primkey-field>key</primkey-field>
</entity>
</enterprise-beans>
</ejb-jar>
————————————————————
weblogic-ejb-jar.xml
====================
<?xml
version="1.0" ?>
<!DOCTYPE
weblogic-ejb-jar PUBLIC
"-//BEA
Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN"
"http://www.bea.com/servers/wls510/dtd/weblogic-ejb-jar.dtd">
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>customerBean</ejb-name>
<persistence-descriptor>
<persistence-type>
<type-identifier>WebLogic_CMP_RDBMS</type-identifier>
<type-version>5.1.0</type-version>
<type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
</persistence-type>
<persistence-us