Page 2 of 3
Moreover, the move to the EJB 2.0 CMP model will protect your investment. By adopting CMP, you minimize the move to next-generation, specification-compliant containers; you also allow the container to regenerate the code behind the scenes, and EJBGen to automatically produce specification-compliant XML deployment descriptors. You therefore automatically take advantage of container improvements. Exciting updates, especially on the security side, are currently being defined for the EJB 2.1 and J2EE (Java 2 Platform, Enterprise Edition) 1.4 specifications. (See Java Specification Requests JSR 151 and JSR 153.)
Okay, I'll stop preaching; now let me prove it.
EJB 2.0 provides some sample code to illustrate the relationships and local interface concepts in the EJB 2.0 CMP model. The section 10.3.12 describes this example as an:
...Order entity bean with relationships to line items and customers, which are other entity beans within the same local scope.Productis indirectly related toOrderby means of the relationship betweenLineItemandProduct.
We will simply implement this example. I've tested the source code for this article on the Win2K platform with the following tools:
After a short introduction to relationships, we will create the tables in the database that represent Order, LineItem, Customer, Product, and Address. Steps 1 and 2 describe how to implement your entity beans without relationships using EJBGen. Step 3 defines those relationships.
As a bonus, I will provide an EJB 2.0 business method. This business method adds a LineItem to an Order and demonstrates the use of value objects and local interfaces.
To understand how to create your database tables, you must first understand how the EJB 2.0 model creates relationships and the implication of those relationships on the tables. For beginners, I recommend you develop the entity beans without relationships, and then add the relationships once the beans have been unit tested. Obviously, the more you gain experience with the 2.0 model, the more easily you can develop the beans with their relationships from the start.
The following list describes the relationship types and their physical mappings:
Order can have multiple LineItems. The foreign key is in the LineItem table and references the primary key in the Order table.
|
The following diagram represents the Order, Customer, LineItem, Product, and Address tables, plus the relationships between them.