July 22, 2018
Hot Topics:

Creating and Deploying Stateless Session Beans and CMP Entity Beans

  • September 9, 2004
  • By Olexiy & Alexander Prokhorenko
  • Send Email »
  • More Articles »

You probably will never believe this, but we are finished with the source code; the only things left are the configuration files for our JAR, WAR, and EAR. But stop! We also need to create a MySQL database, a user for this database, and create a table "for" our entity beans. For this purpose, you can use MySQL Control Center tool (if you are using MySQL on a Windows computer) or the simple mysqladmin tool. Of course, you also can just simply use the next SQL queries to create the database, new user, and table. Here they are:


These queries will help you to create the database (first query), make user userjboss able to work with the newly created database jbossdb (second query), and create this new user userjboss with a password of zzz (third query).

Now, use the following query to create the table:

USE jbossdb;
   story_id integer, 
   pub_date timestamp

Okay; now databases, user, tables—everything seems to be ready. Let's continue with the configuration files for JBoss. We need to create src/WEB-INF/web.xml for Tomcat (which is with JBoss). Here it is:

<?xml version="1.0" encoding="UTF-8"?>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<web-app />

It's empty, yes. We are using JSP, and do not have something to configure and to put into the Web container's configuration file. But we need to have it because it's required by the J2EE spec (of course, you know that!).

Now, we will create the next configuration files—src/META-INF/application.xml:

<?xml version="1.0" ?>
<!DOCTYPE application PUBLIC
"-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN"
   <display-name>Teste and Story</display-name>
   <!-- EJB module -->
      <!-- Web module -->

and the configuration file—src/META-INF/jboss.xml:

<?xml version="1.0" encoding="UTF-8"?>
"-//JBoss//DTD JBOSS 3.0//EN" 
   <resource-managers />

Here, we define JNDI names for our session stateless bean and CMP entity bean. The names are pretty easy, so if you even never saw this configuration file before, you will be able to understand everything using your own simple logic.

The next configuration file is—src/META-INF/ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" 
   <description>Teste and Story</description>
   <display-name>Teste and Story</display-name>
         <display-name>Teste Session Stateless Bean</display-name>
         <display-name>Story Entity CMP Bean</display-name>
   <assembly-descriptor />

I want you to pay attention to the <entity> section. Just take a look and keep in your mind the last four lines in this section. We are defining the primary key, and describing fields of our entity bean.

Here is next configuration file—src/META-INF/jbosscmp-jdbc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC 
"-//JBoss//DTD JBOSSCMP-JDBC 3.0//EN" 

Don't worry about the first part of this file, but again, pay attention to the bottom part. Here, we are defining what each field from entity bean means compared to the database table. So, as you can see, we are saying that the storyId field is story_id column from our table, and pubDate is pub_date in the database. Besides, all descriptions of fields are informative enough, and do not demand further explanation. Certainly, for a deeper understanding of their essence, you should study the EJB specification, but that's beyond the scope of this article.

And one last configuration file is src/META-INF/mysql-ds.xml. You can take it from C:\jboss-3.2.3\docs\examples\jca\mysql-ds.xml (we assume that JBoss is installed in this directory) and modify it to have the correct database name, user's name, and password. Or, you can just use the following one:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ========================================================== -->
<!--                                                            -->
<!--  JBoss Server Configuration                                -->
<!--                                                            -->
<!-- ========================================================== -->
<!-- $Id: mysql-ds.xml,v 1.1 2002/07/22 22:57:24 d_jencks Exp $ -->
<!-- ========================================================== -->
<!--  Datasource config for MySQL using 2.0.11 driver           -->
<!-- ========================================================== -->

Well, we are almost done. We need to compile everything, create JAR, WAR, and EAR, and deploy. Please check that JBoss is running, so you will see in its logs how everything will be deployed. Change the directory to the root directory where you placed all files, where build.xml is located, and type:

ant all

If everything was done correctly, you should see the same screen as the one below:

Click here for a larger image.

Please check your JBoss logs and you will see that everything was deployed correctly (like below):

Click here for a larger image.

This is a really huge portion of different debug information; do not worry about understanding it all. Just check that everything was deployed correctly, with no errors. And now, at least, when everything is deployed, you can open your favourite browser and type this next URL, http://localhost:8080/teste/index1.jsp, and you will see the following screen:

Click here for a larger image.

Then, type http://localhost:8080/teste/index2.jsp and you will get almost the same screen (yup, I've run this page many times, so I already have a lot of entities created!). On the left, and in bold, you will see the entities' ID and the creation date; all fields that have an entity bean.

Click here for a larger image.

Of course, now you will want to check that these entities "forced" to create equal records in the MySQL database. If you will execute any database client and execute the next query:

SELECT * FROM jbossdb.story;

you will see the following:

Click here for a larger image.

Oh, well! My congratulations; we succeeded! As you can see, applications that use CMP entity beans quickly become complex. The source code itself remains simple and easy, but the configuration files for the application server still make problems for newbies and even for rather experienced developers. But, from my own experience, I know that sometimes it's much easier to see something in practice, force it to work, and only after that get deeper into understanding all the parts.

I hope that this article will help you to understand how to use the JBoss application server in practice, how to deploy a session bean, and how to make a CMP entity bean work with help of MySQL. EJB technology possesses a lot of advantages and various unbelievable opportunities. It is important to learn how to use them in practice, even with the help of the simplest applications.

Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date