Exercise 6.3: A Simple Relationship in CMP 2.0

The example program in Exercise 6.3 shows how to implement a simple CMP relationship between the Customer EJB and the Address EJB. The client again uses dependent value classes, to pass address information along to the Customer EJB.

Build and Deploy the Example Programs

Perform the following steps:

  1. Open a command prompt or shell terminal and change to the ex06_3 directory created by the extraction process.

  2. 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_3> set JAVA_HOME=C:jdk1.4.2 C:workbookex06_3> set JBOSS_HOME=C:jboss-4.0
    Unix:$ export JAVA_HOME=/usr/local/jdk1.4.2 $ export JBOSS_HOME=/usr/local/jboss-4.0
  3. Add ant to your execution path.

    Windows:C:workbookex06_3> set PATH=..antin;%PATH%
    Unix:$ export PATH=../ant/bin:$PATH
  4. 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.

Examine the JBoss-Specific Files

The Customer-Address relationship in this example can be mapped to a database table by defining the mapping in jbosscmp-jdbc.xml.

jbosscmp-jdbc.xml

<jbosscmp-jdbc>
...
</enterprise-beans>
<relationships>
   <ejb-relation>
      <ejb-relation-name>Customer-Address</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
         <ejb-relationship-role-name>Customer-has-a-Address
            </ejb-relationship-role-name>
         <key-fields/>
      </ejb-relationship-role>
      <ejb-relationship-role>
         <ejb-relationship-role-name>Address-belongs-to-Customer
            </ejb-relationship-role-name>
         <key-fields>
            <key-field>
                     <field-name>id</field-name>
                     <column-name>HOME_ADDRESS</column-name>
                     </key-field>
          </key-fields>
       </ejb-relationship-role>
    </ejb-relation>
</relationships>
</jbosscmp-jdbc>

To define the mapping of a relationship to a database table, you must define <key-fields>. The <field-name> tag must be the primary key field of the entity bean in the relationship. Thus above, the id <field-name> corresponds to the Address EJB’s primary key field. You can define the <column-name> field to be whatever the column name is in the database. Based on the mappings defined in this file, the Customer table would look like this:

CREATE TABLE CUSTOMER 
(ID INTEGER NOT NULL, 
LAST_NAME VARCHAR(256), 
FIRST_NAME VARCHAR(256), 
HAS_GOOD_CREDIT BIT NOT NULL, 
HOME_ADDRESS INTEGER, 
CONSTRAINT PK_CUSTOMER PRIMARY KEY (ID))

For details on more complex optimizations and database-to-relationship mappings, please see the JBoss CMP 2.0 documentation available at http://www.jboss.org.

Examine and Run the Client Applications

The example program, Client_63, shows how to create a Customer EJB and set the Address relation on that customer.

AddressBean.java

public abstract class AddressBean implements javax.ejb.EntityBean 
{
   private static final int IDGEN_START =
                                  (int)System.currentTimeMillis( );
   private static int idgen = IDGEN_START;

   public Integer ejbCreateAddress (String street, String city, 
                                    String state,  String zip )
      throws CreateException
   {
      setId(new Integer(idgen++));
      setStreet(street);
      setCity(city);
      setState(state);
      setZip(zip);
      return null;
   }
   ...
}

JBoss CMP does have automatic primary-key generation. For this and subsequent examples, though, a very crude ID generator has been created to provide a more predictable mechanism for creating keys. The code just takes the current time in milliseconds at the load of the bean and increments it by one at every ejbCreate( ). Crude, workable for these examples, but not recommended for real applications.

In order to run Client_63 , invoke the Ant task run.client_63. Remember to set your JBOSS_HOME and PATH environment variables.

The output should look something like this:

C:workbookex06_3>ant run.client_63
Buildfile: build.xml

prepare:

compile:

ejbjar:

run.client_63:
     [java] Creating Customer 1..
     [java] Creating AddressDO data object..
     [java] Setting Address in Customer 1...
     [java] Acquiring Address data object from Customer 1...
     [java] Customer 1 Address data:
     [java] 1010 Colorado
     [java] Austin,TX 78701
     [java] Creating new AddressDO data object..
     [java] Setting new Address in Customer 1...
     [java] Customer 1 Address data:
     [java] 1600 Pennsylvania Avenue NW
     [java] DC,WA 20500
     [java] Removing Customer 1...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.47