This exercise begins walking you through the intricacies of CMP 2.0. In this chapter, you will learn more detailed JBoss CMP 2.0 configuration mechanisms by creating the Customer EJB described in the EJB book.
If you already have JBoss running, there is no reason to restart it. Otherwise, start it up as instructed in the JBoss Installation and Configuration chapter.
The database table for this exercise will automatically be created in JBoss’s default database, HypersonicSQL, when the EJB JAR is deployed.
Perform the following steps:
Open a command prompt or shell terminal and change to the
ex06_1
directory created by the extraction
process
Set the JAVA_HOME
and
JBOSS_HOME
environment variables to point to where
your JDK and JBoss 4.0 are installed. Examples:
Windows:
C:workbookex06_1> set JAVA_HOME=C:jdk1.4.2 C:workbookex06_1> set JBOSS_HOME=C:jboss-4.0
|
Unix:
$ export JAVA_HOME=/usr/local/jdk1.4.2 $ export JBOSS_HOME=/usr/local/jboss-4.0
|
Add ant
to your execution path.
Windows:
C:workbookex06_1> set PATH=..antin;%PATH%
|
Unix:
$ export PATH=../ant/bin:$PATH
|
Perform the build by typing ant
.
As in the last exercise, you will see titan.jar
rebuilt, copied to the JBoss deploy
directory,
and redeployed by the application server.
In this section, we introduce a new JBoss CMP 2.0 deployment
descriptor, jbosscmp-jdbc.xml
. This file
provides more detailed control of your bean’s
database mapping as well as more advanced performance-tuning options.
<jbosscmp-jdbc> <defaults> <datasource>java:/DefaultDS</datasource> <datasource-mapping>Hypersonic SQL</datasource-mapping> <create-table>true</create-table> <remove-table>true</remove-table> </defaults> <enterprise-beans> <entity> <ejb-name>CustomerEJB</ejb-name> <table-name>Customer</table-name> <cmp-field> <field-name>id</field-name> <column-name>ID</column-name> </cmp-field> <cmp-field> <field-name>lastName</field-name> <column-name>LAST_NAME</column-name> </cmp-field> <cmp-field> <field-name>firstName</field-name> <column-name>FIRST_NAME</column-name> </cmp-field> <cmp-field> <field-name>hasGoodCredit</field-name> <column-name>HAS_GOOD_CREDIT</column-name> </cmp-field> </entity> </enterprise-beans> </jbosscmp-jdbc>
The <datasource>
configuration variable
tells JBoss’s CMP engine what database connection
pool to use for the entity beans defined in this JAR.
<datasource>java:/DefaultDS</datasource>
It is currently configured to use the default data source defined in
$JBOSS_HOME/server/default/deploy/hsqldb-service.xml
,
but you can change it to your own defined data sources. The
workbook’s Appendix goes into more detail on how to
configure your own data sources.
This variable describes the database mapping that CMP should use:
<datasource-mapping>Hypersonic SQL</datasource-mapping>
Here are some other mappings you could use (this list is not exhaustive):
<datasource-mapping>Oracle8</datasource-mapping> <datasource-mapping>Oracle7</datasource-mapping> <datasource-mapping>MS SQLSERVER</datasource-mapping> <datasource-mapping>MS SQLSERVER2000</datasource-mapping>
For other available supported database mappings, please review JBoss’s advanced documentation on its web site at http://www.jboss.org.
When the <create-table>
configuration
variable is set to true
, JBoss creates the
database tables for each entity bean defined in the deployment
descriptor unless these tables already exist. This create action is
triggered when the EJB JAR is deployed:
<create-table>true</create-table>
When the <remove-table
> configuration
variable is set to true
, JBoss drops the database
tables for each entity bean defined in the deployment descriptor.
This remove action is triggered when the EJB JAR is redeployed or
undeployed:
<remove-table>true</remove-table>
There’s an XML fragment
<entity></entity>
for each entity bean
defined in this EJB JAR. The <ejb-name>
variable defines the entity bean that is described in that section:
<ejb-name>CustomerEJB</ejb-name>
The <table-name>
variable defines what
database table this entity bean should map to:
<table-name>Customer</table-name>
Each <cmp-field>
section describes the
mapping between an entity bean’s fields and the
corresponding columns of the database table. The
<field-name>
tag is the entity bean
field’s name, while the
<column-name>
defines the table
column’s name:
<cmp-field> <field-name>id</field-name> <column-name>ID</column-name> </cmp-field>
There is only one client application for this exercise,
Client_61
. It is modeled after the example in
the EJB book. It creates Customer EJBs in the database based on the
command-line parameters.
To run the client, first set your JBOSS_HOME
and
PATH
environment variables appropriately. Then
invoke the provided wrapper script to execute the program. For each
customer, you must supply on the command line a set of values for
primary key, first name, and last name, as shown here:
Client_61 777 Bill Burke 888 Sacha Labourey
The output of this execution should be:
C:workbookex06_1>client_61 777 Bill Burke 888 Sacha Labourey Buildfile: build.xml prepare: compile: ejbjar: run.client_61: [java] 777 = Bill Burke [java] 888 = Sacha Labourey
When it finishes, the example program removes the created beans, so no data remains in the database.