Instantiating an entity bean

An entity bean represents a row in a database relation. In the home interface, create methods create a row in the database, and finder methods return one or more instances that represent existing rows. All create methods can raise javax::ejb::CreateException, and finder methods can raise javax::ejb::FinderException. The example below first tries to find an existing row using findByPrimaryKey, and creates a row if javax::ejb::FinderException is thrown. The entity bean in this example represents customer credit data. The primary key, bookStore::custCreditKey, has two string fields, custName and creditType. The IDL remote interface is bookStore::custMaintenance:

// Initialize a primary key for the bean
bookStore::custCreditKey custPk;
custPk.custName = CORBA::string_dup(customer_name); 
custPk.creditType = CORBA::string_dup(credit_type);

bookStore::custMaintenance_var customer;
long balance = 2000; 

// Look for an existing instance.
try {  
    cout << "Looking for customer named " << customer_name << "\n";
    customer = home->findByPrimaryKey(custPk);
} catch (javax::ejb::FinderException &fe)
{
    // Instance does not exist. Create it.
    cout << "Customer " << customer_name << " does not exist. "
        << "Creating " << customer_name << " with initial balance of "
        << balance << ".\n";
    customer = home->create(customer_name, balance);
}catch (javax::ejb::FinderException &fe)
{
    cout << "Error creating account for customer " << customer_name ;
}