web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Container-Managed Persistence in Borland Enterprise Server Note or: where a = b a = b The Container converts a = b to the same where clause, where a = b. However, it leaves unmodified an empty string defined as “” . The empty string makes it easy to specify the findAll() method. When you specify just an empty string, the Container construes that to mean the following: select [values] from [table]; Such a select statement would return all values from a particular table. Parameter substitution Parameter substitution is an important part of the where clause. The Borland EJB Container does parameter substitution wherever it finds the standard SQL substitution prefix colon (:). Each parameter for substitution corresponds to a name of a parameter in the finder specification found in the XML descriptor. For example, in the XML deployment descriptor, you might define the following finder method which takes a parameter balance (note that balance is preceded by a colon): findAccountsLargerThan(float balance) balance > :balance The Container composes a SQL select statement whose where clause is: balance > ? Note that the :balance parameter in the deployment descriptor becomes a question mark (?) in the equivalent SQL statement. When invoked, the Container substitutes the value of the parameter :balance for the ? in the where clause. Compound parameters The Container also supports compound parameters; that is, the name of a table followed by a column within the table. For this, it uses the standard dot (.) syntax, where the table name is separated from the column name by a dot. These parameters are also preceded by a colon. For example, the following finder method has the compound parameters :address.city and :address.state: findByCity(Address address) city = :address.city AND state = :address.state The where clause uses the city and state fields of the address compound object to select particular records. The underlying Address object could have Java Beans-style getter methods that correspond to the attributes city and state. Or, alternatively, it could have public fields that correspond to the attributes. Entity beans as parameters An entity bean can also serve as a parameter in a finder method. You can use an entity bean as a compound type. To do so, you must tell the CMP engine which field to use from the entity bean's passed reference to the SQL query. If you do not use the entity bean as a compound type, then the Container substitutes the bean's primary key in the where clause. For example, suppose you have a set of OrderItems entity beans associated with an Order entity object. You might have the following finder method: Chapter 14: Entity Beans and CMP 1.1 in Borland Enterprise Server 117

Container-Managed Persistence in Borland Enterprise Server java.util.Collection OrderItemHome.findByOrder(Order order); This method returns all OrderItems associated with a particular Order. The deployment descriptor entry for its where clause would be: findByOrder(Order order) order_id = :order[ejb/orders] To produce this where clause, the Container substitutes the primary key of the Order object for the string :order[ejb/orders]. The string between the brackets (in this example, ejb/orders) must be the corresponding to the home of the parameter type. In this example, ejb/orders corresponds to an pointing to OrderHome. When you use an EJBObject as a compound type (using the dot notation), you are actually accessing the underlying get method for the field in the definition. For example, the following in the definition: order_id = :order.orderId calls the getOrderId() method on the order EJBObject and uses the result of the call in the selection criterion. Specifying relationships between entities Relational databases (RDBMS) permit records in one table to be associated with records in another table. The RDBMS accomplishes this using foreign keys; that is, a record in one table maintains a field (or column) that is a foreign key or reference to (usually) the primary key of a related record in another table. You can map these same references among entity beans. For the CMP engine to map references among entity beans, you use an entry in the deployment descriptor. The maps field names to their corresponding entities. The CMP engine uses this information in the deployment descriptor to locate the field's associated entity. (Refer to the pigs example for an illustration of the entry.) Any container-managed persistence field can correspond to a foreign key field in the corresponding table. When you look at the entity bean code, these foreign key CMP fields appear as object references. For example, suppose you have two database tables, an address table and a country table. The address table contains a reference to the country table. The SQL create statements for these tables might look as shown below. create table address ( addr_id number(10), addr_street1 varchar2(40), addr_street2 varchar(40), addr_city varchar(30), addr_state varchar(20), addr_zip varchar(10), addr_co_id number(4) * foreign key * ); create table country ( co_id number(4), co_name varchar2(50), co_exchange number(8, 2), co_currency varchar2(10) ); Note that the address table contains the field addr_co_id, which is a foreign key referencing the country table's primary key field, co_id. 118 BES Developer’s Guide

Container-Managed Persistence in <strong>Borland</strong> Enterprise Server<br />

java.util.Collection OrderItemHome.findByOrder(Order order);<br />

This method returns all OrderItems associated with a particular Order. The deployment<br />

descriptor entry for its where clause would be:<br />

<br />

findByOrder(Order order)<br />

order_id = :order[ejb/orders]<br />

<br />

To produce this where clause, the Container substitutes the primary key of the Order<br />

object for the string :order[ejb/orders]. The string between the brackets (in this<br />

example, ejb/orders) must be the corresponding to the home of the<br />

parameter type. In this example, ejb/orders corresponds to an pointing to<br />

OrderHome.<br />

When you use an EJBObject as a compound type (using the dot notation), you are<br />

actually accessing the underlying get method for the field in the definition. For<br />

example, the following in the definition:<br />

order_id = :order.orderId<br />

calls the getOrderId() method on the order EJBObject and uses the result of the call in<br />

the selection criterion.<br />

Specifying relationships between entities<br />

Relational databases (RDBMS) permit records in one table to be associated with<br />

records in another table. The RDBMS accomplishes this using foreign keys; that is, a<br />

record in one table maintains a field (or column) that is a foreign key or reference to<br />

(usually) the primary key of a related record in another table. You can map these same<br />

references among entity beans.<br />

For the CMP engine to map references among entity beans, you use an <br />

entry in the deployment descriptor. The maps field names to their<br />

corresponding entities. The CMP engine uses this information in the deployment<br />

descriptor to locate the field's associated entity. (Refer to the pigs example for an<br />

illustration of the entry.)<br />

Any container-managed persistence field can correspond to a foreign key field in the<br />

corresponding table. When you look at the entity bean code, these foreign key CMP<br />

fields appear as object references.<br />

For example, suppose you have two database tables, an address table and a country<br />

table. The address table contains a reference to the country table. The SQL create<br />

statements for these tables might look as shown below.<br />

create table address (<br />

addr_id number(10),<br />

addr_street1 varchar2(40),<br />

addr_street2 varchar(40),<br />

addr_city varchar(30),<br />

addr_state varchar(20),<br />

addr_zip varchar(10),<br />

addr_co_id number(4) * foreign key *<br />

);<br />

create table country (<br />

co_id<br />

number(4),<br />

co_name varchar2(50),<br />

co_exchange number(8, 2),<br />

co_currency varchar2(10)<br />

);<br />

Note that the address table contains the field addr_co_id, which is a foreign key<br />

referencing the country table's primary key field, co_id.<br />

118 BES Developer’s Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!