|
Entity CMP with Container-Managed Relationships
|
This lab's objective is to develop an Entity Bean Container-Managed Persistence (CMP)
with Container-Managed Relationships (CMR).
We are going to implement the following class diagram:
Order has a 1-n relationship with Items (An Order can have one or more items).
We will see that the container manages those relationships for us!
Here is the code
For a bi-directionnal relationship, one can "navigate" from one Entity to the other, and vice-versa.
In order to "navigate" from one side to the other, we will use CMRs through getters and setters.
An "Order" can have one or more "Items"
OrderBean becomes:
public abstract Collection getItems();
public abstract void setItems(Collection items);
An "Item" can only have one "Order"
ItemBean becomes:
public abstract OrdersLocal getOrder();
public abstract void setOrder(OrdersLocal orderLocal);
You have probably noticed that we are using the OrdersLocal interface, which means
that the "Item" table contains a foreign key on the primary key table "Order".
That's a;;! The code generation work will be done by XDoclet.
The only difference with our previous CMP lab is the tags definition tied to getters
getItems.
We will use the @ejb.relation tag and the propriatary one @jboss:target-relation.
The Order class becomes:
/**
* getItems
*
* Returns a collection of items (via its Local interface)
*
* @ejb.interface-method
*
* @ejb.relation
* name="order-items"
* role-name="order-has-items"
*
* @return a Collectionn of items
*/
public abstract Collection getItems();
, et dans la classe Item:
/**
* @ejb.interface-method
*
* @ejb.relation
* name="order-items"
* role-name="item-has-order"
* cascade-delete="yes"
*
* @jboss.relation
* related-pk-field="orderid"
* fk-column="orderid_fk"
*
* @return a Collectionn of items
*/
public abstract OrdersLocal getOrder();
Your turn to code!
Adding Items to an Order
Once an Order has been created, where to add the related Items?
You just have to create a remote method that takes as arguments the necessary data to create
an Item, then to use the CMR getter or Order in order to add the newly created Item.
(There are other possible designs).
public void addItem(args....) {
Context ctx = new InitialContext();
Object obj = ctx.lookup("org.jyperion.sample.j2ee.ebcmr.ItemBeanLocal");
ItemLocalHome itemLocalHome = (ItemLocalHome)obj;
ItemLocal itemLocal = itemLocalHome.create(...);
this.getItems().add(itemLocal); // <- CMR getter
}
A few finders
Write the following finders using XDoclet:
- findAll: Returns all "Orders"
- findAllOrdersWithItemDescription: Returns all "Orders" with "Items" with a specific description.
- findAllOrdersWithNoItemDescription: Returns all "Orders" with "Items" without any description.
|
Many-to-many relationship
|
Some code is provided to you in the following package: org.jyperion.j2ee.sample.many2many.
Customer, Order, LineItem, Product Entity Beans
By creating a new project, implement the following schema:
Good luck!