WebLogic Server 6.1 Code Examples, BEA Systems, Inc.

Package examples.ejb20.cascadeDelete.one2many

This example is a package that demonstrates the removal of information using cascade delete with the Oracle database.

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.
Bank  
BankHome  
Customer Interface for a customer bean.
CustomerHome This interface is the home interface for the EJBean CustomerBean.
 

Class Summary
AccountBean AccountBean is an EntityBean.
BankBean  
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.cascadeDelete.one2many Description

This example is a package that demonstrates the removal of information using cascade delete with the Oracle database. It uses the one-to-many relationships between two EJB's written to the EJB 2.0 specification.The example includes entity EJBeans calledAccountBean, BankBean, and CustomerBean.

The example demonstrates:

Pre-Built Examples

The EJB 2.0 examples included in the WebLogic Server 6.1 distribution are not shipped pre-built.To run these examples, you must build the example following the specific examples instructions.

Client application

TheClientapplication 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. Removes customer[0]
  7. Displays customer associated with all the accounts
  8. Displays each account associated with the customer
  9. Remove all accounts and customers

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

Set up the example

Take a look at the XML deployment files ejb-jar.xml, weblogic-ejb-jar.xml and weblogic-cmp-rdbms-jar.xml to see the generalstructure of the EJBean, which classes are used for the differentobjects and interfaces, then look at Client code to see how the application works.

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

You'll need to create the following tables in your Oracle database using the following SQLstatements :

"create table cascade_Customers(cust_name varchar(20) constraint pkCustCD primary key, acct_id varchar(20), cust_age integer, cust_level integer, cust_last date);""create table cascade_Accounts(acct_id varchar(20) constraint pkAcctCD primary key, bal numeric, type varchar(20), cust_name varchar(20) CONSTRAINT fk_cust_name REFERENCES cascade_Customers(cust_name) ON DELETE CASCADE);""commit;"

Note: that the persistent storage is completely invisible to theclient; the actual storage is handled automatically by the containerand not by the EJBean. All database properties, such as the loginname 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 inSetting up your environment.

We provide a build script for you to build the example:

The build script is build.xml.

Run the build script using the following Java ant command:
        $ant

The script will build the example and place the files in thecorrect locations in your WebLogic Server distribution:

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

Additional information on using the build scriptsis 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 .

    Although a sample connection pool is provided for many of the EJB 2.0 examples, for this example, you must make sure that your sample connection pool, called demoPool points to the oraclePool.

    You'll need to:

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

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

Run the example

  1. Run the client in a separate command line window. Set up your client as described inSetting up your environment,and then run the client by entering:
    $ java examples.ejb20.cascadeDelete.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.cascadeDelete.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 from WebLogic Server similar to this:
    Beginning examples.ejb20.cascadeDelete.one2many.Client...Create Accounts and customers: Creating account account0 with a balance of 0.0 account type null... Account account0 successfully created Creating customer customer0... Customer customer0 successfully createdCreating account account1 with a balance of 1000.0 account type Savings...Account account1 successfully createdCreating customer customer1...Customer customer1 successfully createdCreating account account2 with a balance of 2000.0 account type null...Account account2 successfully createdCreating customer customer2...Customer customer2 successfully createdCreating account account3 with a balance of 3000.0 account type Savings...Account account3 successfully createdCreating customer customer3...Customer customer3 successfully createdCreating account account4 with a balance of 4000.0 account type null...Account account4 successfully createdCreating customer customer4...Customer customer4 successfully createdCreated Five Customers and Five Accounts Establish One-to-Many Relationships between Customer and Accounts: Add account account0 to customer0 Add customer customer0 to account account0 Add account account1 to customer0 Add customer customer0 to account account1 Add account account2 to customer0 Add customer customer0 to account account2 Add account account3 to customer3 Add customer customer3 to account account3 Add account account4 to customer3 Add customer customer3 to account account4 Print Customers: Customer: customer0 has accounts: 	account2 with balance 2000.0 	account1 with balance 1000.0 	account0 with balance 0.0 Customer: customer1 has accounts: Customer: customer2 has accounts: Customer: customer3 has accounts: 	account4 with balance 4000.0 	account3 with balance 3000.0 Customer: customer4 has accounts: Print Accounts: Account: account0 has customers: customer0 Account: account1 has customers: 	customer0 Account: account2 has customers: 	customer0 Account: account3 has customers: 	customer3 Account: account4 has customers: 	customer3 Cascade Delete: Removing customer 0 and cascade delete its accounts customer0 removed Print Customers: Customer: customer0 not found Customer: customer1 has accounts: Customer: customer2 has accounts: Customer: customer3 has accounts: 	account4 with balance 4000.0 	account3 with balance 3000.0 Customer: customer4 has accounts: Print Accounts: Account: account0 not found Account: account1 not found Account: account2 not found Account: account3 has customers: 	customer3 Account: account4 has customers: 	customer3 cleanup... customer1 removed customer2 removed customer3 removed customer4 removed Done cleanup... 
  3. You should receive output from the client application similar to this:
    Beginning examples.ejb20.one2many.Client...Creating bank ... Bank successfully created Create Accounts and customers: Creating account account0 with a balance of 0.0 account type null... Account account0 successfully created Creating customer customer0... Customer customer0 successfully createdCreating account account1 with a balance of 1000.0 account type Savings...Account account1 successfully createdCreating customer customer1...Customer customer1 successfully createdCreating account account2 with a balance of 2000.0 account type null...Account account2 successfully createdCreating customer customer2...Customer customer2 successfully createdCreating account account3 with a balance of 3000.0 account type Savings...Account account3 successfully createdCreating customer customer3...Customer customer3 successfully createdCreating account account4 with a balance of 4000.0 account type null...Account account4 successfully createdCreating customer customer4...Customer customer4 successfully created Establish One-to-Many Relationships between Customer and Accounts:Add account account0 to customer0 Add customer customer0 to account account0 Add account account1 to customer0 Add customer customer0 to account account1 Add account account2 to customer0 Add customer customer0 to account account2 Add account account3 to customer3 Add customer customer3 to account account3 Add account account4 to customer3 Add customer customer3 to account account4 Print Customers and Accounts Account: account0 has customers:         customer0Account: account1 has customers:         customer0Account: account2 has customers:         customer0Account: account3 has customers:         customer3Account: account4 has customers:         customer3Cascade Delete: Removing customer 0 and cascade delete its accounts customer0 removedPrint Customers and Accounts Print Customers:Customer: customer0 not foundCustomer: customer1 has accounts:Customer: customer2 has accounts:Customer: customer3 has accounts:          account4 with balance 4000.0          account3 with balance 3000.0Customer: customer4 has accounts:Print Accounts:Account: account0 not foundAccount: account1 not foundAccount: account2 not foundAccount: account3 has customers:         customer3Account: account4 has customers:         customer3Cleanup... End examples.ejb20.one2many.Client...

There's more...Read more about:


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

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