|
http://in.geocities.com/rsramsam/cmpdemo.htm |
ENTITY BEANS
R.S.Ramaswamy
( published
in DeveloperIQ
.....December-2003)
============================================================
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 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
have just 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 and this is
more intuitive.If we have an account bean, we can write code for deposit as
follows:
int n = account1. getBalance();
n=n+400;
account1.setBalance(n);
Doubtless,
this is much simpler and easier than
writing sql code.
------------------------------------------------------------
Entity beans 'persist' (ie) they
are stored in Enterprise server's
hard disk and so even if there is a shutdown of the server, the beans
'survive' and can be created again from
the hard disk storage.[A session bean is not stored in hard
disk].
------------------------------------------------------------
A Session bean , whether it is stateless or stateful is 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'.management as specified. in the Deployment descriptor.
-----------------------------------------------------------
[ Can we use a session
bean for 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
bean just 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
If we think of an entity bean instance as a
record in a table of database, it automatically follows that it
should have 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
Access database 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 of String type)
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 records have
to 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.
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
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?
CMP
beans have two advantages:
i) less code.
ii) 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 .
============================================================
EXAMPLE FOR
CMP -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 String
getName() throws
RemoteException;
public void
setName(String s)
throws
RemoteException;
public String getPlace() throws RemoteException;
public
void setPlace(String s)
throws
RemoteException;
}
------------------------------------------------------------
Next we write the home interfcae.
//
**********customerHome.java *******************
import
javax.ejb.*;
import
java.rmi.*;
public
interface customerHome extends EJBHome
{
public
customerRemote create
(String
a, String b, String c)
throws RemoteException, CreateException;
public
customerRemote findByPrimaryKey (String a)
throws RemoteException, FinderException;
}
------------------------------------------------------------
//customerBean.java
import
javax.ejb.*;
import
java.rmi.*;
public
class customerBean implements EntityBean
{
public String key;
public String name;
public String place;
public String getName()
{
return
name;
}
public String getPlace()
{
return place;
}
//---------------------------
public void setName(String b)
{
name=b;
}
public void setPlace(String c)
{
place=c;
}
//-------------------------------
public String ejbCreate
(String a, String
b, String c)
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 {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void ejbLoad() {}
public void ejbStore() {}
public void setEntityContext(EntityContext
ec) { }
public void unsetEntityContext() { }
}
------------------------------------------------------------
We now
create three xml 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-use>
<type-identifier>WebLogic_CMP_RDBMS</type-identifier>
<type-version>5.1.0</type-version>
</persistence-use>
</persistence-descriptor>
<jndi-name>customerJndi</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
------------------------------------------------------------
weblogic-cmp-rdbms-jar.xml
==========================
<?xml
version="1.0" ?>
<!DOCTYPE
weblogic-rdbms-bean PUBLIC
"-//BEA
Systems, Inc.//DTD WebLogic 5.1.0 EJB RDBMS Persistence//EN"
"http://www.bea.com/servers/wls510/dtd/weblogic-rdbms-persistence.dtd">
<weblogic-rdbms-bean>
<pool-name>demoPool</pool-name>
<table-name>table1</table-name>
<attribute-map>
<object-link>
<bean-field>key</bean-field>
<dbms-column>key</dbms-column>
</object-link>
<object-link>
<bean-field>name</bean-field>
<dbms-column>name</dbms-column>
</object-link>
<object-link>
<bean-field>place</bean-field>
<dbms-column>place</dbms-column>
</object-link>
</attribute-map>
<finder-list>
</finder-list>
<options>
<use-quoted-names>false</use-quoted-names>
</options>
</weblogic-rdbms-bean>
------------------------------------------------------------
We are now
ready to create the jar file .
It is
assumed that we are in c:\weblogic\beans\cmp folder.
We edit,
customerRemote.java
customerHome.java &
customerBean.java
in
this folder.
Now, we create a subfolder under cmp
This
folder is named 'META-INF'.
We
create in this folder, the following xml
files.:
ejb-jar.xml
weblogic-ejb-jar.xml &
weblogic-cmp-rdbms-jar.xml
---------------------------------
cmp>SET JAVA-HOME=C:\JDK1.3
>
SET WL_HOME=C:\WEBLOGIC
>set path=c:\windows\command;c:\jdk1.3\bin;c:\weblogic\bin
>set classpath=c:\weblogic\beans\cmp;
c:\weblogic\classes;
c:\weblogic\lib\weblogicaux.jar;
( all
these steps are similar to what we did for session beans.)
------------------------------------------------------------
Now,
we can compile our java source files.
cmp> javac
*.java
-----------------------------
We now
create the jar file.
cmp>
jar cf
customer.jar *.class META-INF\*.xml
--------
The
next step is to deploy.( the target is named 'customer1.jar')
cmp>
ejbc customer.jar customer1.jar
---------
Next
we have to edit c:\weblogic\weblogic.properties file
The
relevant sections have been shown below.
----
# WEBLOGIC
DEMO CONNECTION POOL PROPERTIES
#
--------------------------------------------------
# CLUSTER
USERS: Note that ALL JDBC connection pools should be set # up
in the *per-cluster* properties file ONLY.
#
# This
connection pool uses the sample Cloudscape database shipped
# with
WebLogic. Used by the EJBean, JHTML, JSP and JMS examples.
#
Uncomment to use:
weblogic.jdbc.connectionPool.demoPool=\
# url=jdbc:cloudscape:demo,\
url=jdbc:odbc:customer,\
# driver=COM.cloudscape.core.JDBCDriver,\
driver=sun.jdbc.odbc.JdbcOdbcDriver,\
initialCapacity=1,\
maxCapacity=2,\