Create CMR/CMP Entity Beans
Deploy entity beans to configure a server and import a database structure
by Olexiy Prokhorenko and Alexander Prohorenko

February 24, 2005

As you likely already know, Enterprise JavaBeans (EJBs) constitute a powerful, flexible technology that gives the developer significant advantages during the development process. However, it is not easy to learn, particularly for the beginning of the design and preparation of applications. The same is true for Container-Managed Relationship (CMR) and Container-Managed Persistence (CMP). Of course, once configured and working, development with these technologies becomes more pleasurable.

Creating even small applications demands attention to and knowledge of the configuration of the application server, as well as the EJBs themselves. At present a lot of GUI tools for automatic creation of configuration files and for importing existing structures of databases exist; however, to feel a little more confident in the whole process, and for solving problems, it is necessary to create everything (at least once!) with the help of the simple text editor, which is exactly what we will do here (see the sidebar, "Taking a Clear Approach").

Before getting started, it's important to note that it is recommended you have some understanding of EJB programming because the goal here is to describe how we apply CMR/CMP techniques in practice, and how that application will work on the BEA WebLogic server. Because we're discussing the simple entity bean, we will not be explaining code, but we'll certainly mention areas that have potential pitfalls while developing your own code. To complete the procedures described here, it's important that your environment be set up as described in the sidebar, "Prepare Your Development Environment."

Start with the Database
Let's begin by creating the MySQL user and database. Assuming that MySQL is installed correctly and you have configured the root user and set its password, you will need to run:

C:\mysql\bin\mysql.exe –u root –p

and enter the password. If you skip configuring the root user (while you were installing your MySQL server), you can simply run:

C:\mysql\bin\mysql.exe

When you see the MySQL client screen, type:

CREATE DATABASE zzzdb;
INSERT INTO mysql.db VALUES (
	'localhost','zzzdb','zzzuser',
	'Y','Y','Y','Y','Y','Y','N','Y',
	'Y','Y','Y','Y');
INSERT INTO mysql.user VALUES (
	'localhost','zzzuser',password(
	'zzzpassword'),'N','N','N','N',
	'N','N','N','N','N','N','N','N',
	'N','N','N','N','N','N','N','N',
	'N','','','','',0,0,0);

You see can see an example in Figure 1. Reload MySQL server by running:

C:\mysql\bin\mysqladmin.exe –u
	root –p reload 

or :

C:\mysql\bin\mysqladmin.exe 
	reload 

(The command you use will depend on your root user configuration.) This command will cause your changes to take effect. Be sure to remember these values—user name (zzzuser), user password (zzzpassword) and database name (zzzdb)—you will need them later when you configure the MySQL connection pool and data source in the BEA WebLogic server.

Now we'll need to create a new configuration in WebLogic to be able to work with our projects. For this purpose we'll use BEA's Configuration Wizard, which guides us through the steps to generate the new configuration. It creates a special server with all the working and necessary directories. Once we run it, we will be able to configure it, deploy our applications, and do anything else we want to do with it—it will be our own WebLogic server. To begin, run:

C:\bea\weblogic81\common\bin\
	config.cmd 

(WebLogic should be installed into the default directories you see here; otherwise, you need to correct the path to match your conafiguration). You can also run the Configuration Wizard by selecting Start > Programs > BEA WebLogic Platform 8.1 > Configuration Wizard. Next, choose Create a new WebLogic configuration, click Next, select Basic WebLogic Server Domain template (left side of the screen), click Next, choose Express, and then click Next.

Enter the administrator's credentials. Choose any you want, but do not forget them because you will need to enter your login and password each time you want to make any changes to your server's configuration. Click Next after you have entered the credentials. On the next screen choose Development Mode and (BEA Supplied SDKs) Sun SDK 1.4.2_04 @ C:\bea\jdk142_04. Click Next. Now, on the bottom-right part of the next screen, you will see where you can enter a name for the configuration. Choose any name you want (in this example we typed zzzdomain). Click Create (see Figure 2).

Click Done to finish preparing the work area. We can start our server with this command:

C:\bea\user_projects\domains\
	zzzdomain\startWebLogic.cmd

Or, you can start it by selecting Start > Programs > BEA WebLogic Platform 8.1 > User Projects > zzzdomain > Start Server. To stop your server you can run only:

C:\bea\user_projects\domains\
	zzzdomain\stopWebLogic.cmd 

At this point the screen should appear similar to what you see in Figure 3.

Configure the Server
Our next task will be to configure our server to create the MySQL connection pool and data source, which we need for CMR/CMP. Start by running the Administration Console for the server. To do so, run your favorite browser and type:

http://localhost:7001/console

You will see the Web page shown in Figure 4. Type your login and password (which you created previously). Click Sign In to see a screen divided into two frames. In the frame on the right, find the Services Configurations - JDBC section and click on Connection Pools. In the same frame click Configure a new JDBC Connection Pool. Next, select Database Type – MySQL, and Database Driver – MySQL's Driver (Type 4) Versions:using com.mysql.jdbc.Driver. Click Continue to see the screen shown in Figure 5. Next, type zzzdb for the database name, localhost for the host name, zzzuser for the database user name, and zzzpassword for the password. Recall that you created these previously when you added the MySQL user.

Leave the defaults for all of the other fields on this screen. Click Continue and on the next screen click Test Driver Configuration. If everything is okay, you should see the screen shown in Figure 6.

Now click Create and deploy, and then take a look at the frame on the left and select zzzdomain > Services > JDBC > Data Sources. In the frame on the right click Configure a new JDBC Data Source. The only field you should set on this screen is JNDI Name – MySqlDS. It will be used in our application's configuration files. Do not change the other fields; leave them set to their default values. Click Continue. Set (or leave as is) the Pool Name – MyJDBC Connection Pool field (it's the name of the connection pool that was created previously). Click Continue. On the next screen click Create. We're done; now the server is prepared for our code.

Unzip the code download zip file associated with this article (available online at www.WebLogicPro.com) into any directory you want. Enter this directory, but pay close attention because you'll need to open build.xml with a text editor and change C:\bea at the beginning of the file to your BEA WebLogic installation directory. Now save it and just type ant all. Wait for a few seconds while everything is compiled, packaged, and made ready to use. You will find in this directory the testcmr.ear application package, which we will deploy on our WebLogic server. If you did everything correctly you will see the same screen as shown in Figure 7.

Return to WebLogic's Administration Console, sign in, and then in the frame on the right find the Domain Configurations – Your Deployed Resources section. Click Applications, click Deploy a new Application…, and then choose testcmr.ear (see Figure 8). Click Continue and on the next screen click Deploy.

If you did everything correctly you should see two tables, and in both of them the Status of Last Action should indicate "Success." This status indicates that the entity beans were deployed successfully, and we can take a look how it's working. You might wonder why we didn't prepare the tables in our database. The answer is simple—the container will create them automatically.

Browse the Result
Now it's time to test how everything works. Open your browser, and type this URL:

http://localhost:7001/testcmr

You'll see a screen with three options: create records, view current, and delete (but you need to create them first) records. We are writing records; everything will appear as records in your database's tables. From a technical point of view we will be creating entity beans, but not database records directly.

The application consists of two entity beans: Vendor and Model. In our example we will think about car vendors such as Toyota, BMW, and so on. Needless to say, each vendor could have a few models, but any model has only one vendor, which is why our relationship is one-to-many. We also will be using cascade-delete, which means that if you delete the Vendor bean, all Model beans associated with this vendor will be deleted too. Our entity beans use CMP, so you will be able to see how everything works when you monitor changes to your database's tables.

One note about the "database" point of view: each vendor will have an ID and name, and each model will also have its own ID and name as well as a so-called Vendor ID that will contain the ID of the vendor with which this model is associated. This structure is pretty simple.

Getting back to the "visual" part, create an option that will create a few vendors and models and associate the models with the vendors. The View option will show current vendors and models. The Remove option will remove vendors, and this option will remove models associated with them automatically. After clicking on an option you can take a look at the status of your database's tables, which will help you to better understand how it works.

We are almost done, but take note of a few parts of the code and configuration files that could pose a problem for you when you develop your own code. In the Model.java component interface we are exposing the getVendorName() method, but we are not exposing the getVendor() method, which is the getter method for the CMR field. We can't expose CMP fields (it's just not allowed) to a remote client, and we are using the getVendorName() method to provide the getter method of the vendor's name to the client. The same reason applies for Vendor.java; its getAllModels() method is just exposing the getter method for the CMR field's getModels() method to a remote client.

The setVendorid() method in ModelBean.java should never be called by you—never! You do not even use it in the create() method when you are creating the Model field, which will be set automatically by the container when you associate the model with the vendor.

In regard to the configuration file weblogic-cmp-rdbms-jar.xml, in the section <weblogic-rdbms-relation> and subsection <column-map> there are two fields: <foreign-key-column> and <key-column>. Be aware that for these key-columns we are writing the title of columns in the database, not from ejb-jar's <cmp-field>. Moreover, notice that as our <relationship-role-name> is model-has-vendor, <foreign-key-column> is a column in the model's table, and <key-column> is the column in the vendor's table. It is through this column that the model is associated with the vendor.

All that's left is a suggestion: play with our code, do your own changes, and be sure that you understand how everything works. If you have problems, contact us. We'll be glad to help if we can. EJBs bring a lot of advantages, but the technology requires patience to prepare, design, and build so that after we're done we can just enjoy. CMP/CMR is a nice feature, and there's no reason to not use it as much as possible. Remember, too, that there are also one-to-one and many-to-many relationships, except one-to-many, that we'll be working with, but don't worry about them; once you understand the basics, you'll understand everything else.

About the Authors
Olexiy Prokhorenko is a J2EE software developer and a Sun-certified Java programmer, Sun-certified Web component developer, and is in the process of becoming a Sun-certfied enterprise architect. Alexander Prohorenko is a Unix systems and Oracle database administrator and a Sun-certified system administrator. Contact Olexiy at green@prohorenko.com, and contact Alexander at white@prohorenko.com.