WebLogic Server 6.0 Code Examples, BEA Systems, Inc.

Package examples.ejb20.relationships.one2many

This example is a package that demonstrates a one-to-many relationships between two EJB's written to the EJB 2.0 specification.

See:
          Description

Interface Summary
Account The methods in this interface are the public face of AccountBean.
AccountHome This interface is the home interface for the EJBean AccountBean, which in WebLogic is implemented by the code-generated container class AccountBeanC.
Customer Interface for a customer bean.
CustomerHome This interface is the home interface for the EJBean CustomerBean.
 

Class Summary
AccountBean AccountBean is an EntityBean.
Client This class demonstrates CMP relationships and EJB-QL by performing the following:
CustomerBean Represents a customer and demonstrates one to many releationship with AccountBean.
 

Package examples.ejb20.relationships.one2many Description

This example is a package that demonstrates a one-to-many relationships between two EJB's written to the EJB 2.0 specification. The example is an entity EJBean called AccountBean and CustomerBean.

The example demonstrates:

Before Running the EJB 2.0 Examples

Before you can build and run the EJB 2.0 examples, you must first download the WebLogic Server 6.0 supplement, ejb20.jar, from the BEA product download page. This allows you to use the Enterprise JavaBeans features in the WebLogic Server 6.0 environment. Until you install the ejb20.jar file, you will be unable to build and run the EJB 2.0 examples.

Pre-Built Examples

The EJB 2.0 examples included in the WebLogic Server 6.0 distribution are not shipped pre-built. To run these examples, you must first download ejb20.jar and then build the example following the specific examples instructions.

Client application

The Client application performs these steps:
  1. Creates 5 separate accounts and customers
  2. Displays the account type and balances for each account
  3. Adds all accounts to one customer
  4. Displays customer associated with all the accounts
  5. Displays each account associated with the customer
  6. Finds customers by name
  7. Finds customers by importance
  8. Finds all accounts with balances over $2000
  9. Finds the account with a balance of zero
  10. Finds any accounts with a null type
  11. Removes all accounts

To get the most out of this example, first read through the source code files.

Start with the XML deployment files ejb-jar.xml, weblogic-ejb-jar.xml and weblogic-cmp-rdbms-jar.xml to find the general structure of the EJBean, which classes are used for the different objects and interfaces, then look at Client code to see how the application works.

You'll need to set up the persistent storage of the EJBean. You'll use a database for the persistent storage of the entity EJBean. Each instance of an EJBean is written to a row in a table. The table (Accounts and Customers) must be created and exist in the database before the example is run.

If you are using the evaluation copy of Cloudscape that is included with WebLogic Server, this table has already been created in the "demo" database. If you're using a database other than Cloudscape, you'll need to create the table in your database using an appropriate SQL statement such as :

"create table Accounts (acct_id varchar(50) constraint pk_acct primary key, bal numeric, type varchar(50), cust_name varchar(50));"
"create table Customers (cust_name varchar(50) constraint pk_cust primary key, acct_id varchar(50), cust_age integer, cust_level integer, cust_last date);"
"create table CUSTOMER_ACCOUNTS (cust_name varchar(50), acct_id varchar(50));"

Note that the persistent storage is completely invisible to the client; the actual storage is handled automatically by the container and not by the EJBean. All database properties, such as the login name and password, are defined in the connection pool. A preconfigured connection pool is included in the example server.

These three sections cover what to do:

  1. Build the example
  2. Configure the server
  3. Run the example

Build the example

Set up your development shell as described in Setting up your environment.

We provide separate build scripts for Windows NT and UNIX:

These scripts will build the example and place the files in the correct locations in your WebLogic Server distribution:

Running the build script places the EJB in /config/examples/applications, where it automatically deploys once the server is started. If you are already running the server and build another EJB, it is automatically placed in this directory and instantly deployed.

Additional information on using the build scripts is found in Building Enterprise JavaBean examples

Configure the server

  1. Start the WebLogic Server.

  2. Start the Administration Console.

  3. Click to expand the Deployments node in the left pane and select EJB Deployments.

    A list of deployed EJBs displays.

  4. Choose the appropriate deployment unit from the list.

  5. Select the Target tab in the right pane.

  6. Verify that the examplesServer is choosen as the target for the EJB.

  7. Setup a connection pool in the Administration Console .

    For your convenience, a sample connection pool, called demoPool is included with this example. You can use this connection pool with any of the EJB examples.

    You'll need to:

    In this example, the fileRealm for the access control list (ACL) is preconfigured to allow access for everyone.

    If you need more information about how to use connection pools, read Using WebLogic JDBC: Using connection pools.

Run the example

  1. Run the client in a separate command line window. Set up your client as described in Setting up your environment, and then run the client by entering:
    $ java examples.ejb20.relationships.one2many.Client

    If you're not running the WebLogic Server with its default settings, you will have to run the client using:

    $ java examples.ejb20.relationships.one2many.Client "t3://WebLogicURL:Port"

    where:

    WebLogicURL
    Domain address of the WebLogic Server
    Port
    Port that is listening for connections (weblogic.system.ListenPort)

  2. You should receive output similar to this:
    Beginning examples.ejb20.relationships.one2many.Client...
    
    
    Create Accounts and customers:
    Creating account ID: 0 with a balance of 0.0 account type null...
    Account ID: 0 successfully created
    Creating customer customer0...
    Customer customer0 successfully created
    Creating account ID: 1 with a balance of 1000.0 account type Savings...
    Account ID: 1 successfully created
    Creating customer customer1...
    Customer customer1 successfully created
    Creating account ID: 2 with a balance of 2000.0 account type null...
    Account ID: 2 successfully created
    Creating customer customer2...
    Customer customer2 successfully created
    Creating account ID: 3 with a balance of 3000.0 account type Savings...
    Account ID: 3 successfully created
    Creating customer customer3...
    Customer customer3 successfully created
    Creating account ID: 4 with a balance of 4000.0 account type null...
    Account ID: 4 successfully created
    Creating customer customer4...
    Customer customer4 successfully created
    
    Created Five Customers and Five Accounts
    
    Establish One-to-Many Relationships between Customer and Accounts:
    Add account ID: 0 to customer0
    Add customer customer0 to account ID: 0
    Add account ID: 1 to customer0
    Add customer customer0 to account ID: 1
    Add account ID: 2 to customer0
    Add customer customer0 to account ID: 2
    Add account ID: 3 to customer0
    Add customer customer0 to account ID: 3
    Add account ID: 4 to customer0
    Add customer customer0 to account ID: 4
    
    Print Customers:
    Customer: customer0 has accounts:
            ID: 4 with balance 4000.0
            ID: 3 with balance 3000.0
            ID: 2 with balance 2000.0
            ID: 1 with balance 1000.0
            ID: 0 with balance 0.0
    Customer: customer1 has accounts:
    Customer: customer2 has accounts:
    Customer: customer3 has accounts:
    Customer: customer4 has accounts:
    
    Print Accounts:
    Account ID: 0 has customers:
            customer0
    Account ID: 1 has customers:
            customer0
    Account ID: 2 has customers:
            customer0
    Account ID: 3 has customers:
            customer0
    Account ID: 4 has customers:
            customer0
    
    Find By Name
    found- customer0
    found- customer1
    found- customer2
    found- customer3
    found- customer4
    
    Find By Importance
    found- customer0
    found- customer1
    found- customer2
    found- customer3
    found- customer4
    
    Querying for accounts with a balance greater than 2000.0...
    Account ID: 3; balance is $3000.0
    Account ID: 4; balance is $4000.0
    
    Querying for an account with zero balance...
    Account ID: 0; balance is zero
    
    Querying for accounts with a null account type
    Account ID: 0; account type is null
    Account ID: 2; account type is null
    Account ID: 4; account type is null
    Removing beans...
    
    End examples.ejb20.relationships.one2many.Client...
    
    

There's more...

Read more about:


Documentation is available at
http://e-docs.bea.com/wls/docs60

Copyright © 2000 BEA Systems, Inc. All Rights Reserved.