-
1. Re: CMR, three way relationship
beal91 Apr 5, 2004 7:20 AM (in response to glazed)The only way to have the 'type' attribute is to specify a ProductItem entity that is referenced by 1:M relationships from both Product and Item. If you want to be able to access the Item entity directly from the Product entity, I can think of two options.
Option 1: Define a N:M CMR relationship between Product and Item and use jbosscmp-jdbc.xml to map the relationship to the same fields that are used by the ProductItem entity. This will give you three relationships in total -- Product-Item, Product-ProductItem, and Item-ProductItem.
Option 2: Define a non-CMP property in your Product and Item bean classes to allow direct access to the other end of the relationship defined by the ProductItem entities. Your bean class for Product would end up looking something like:public abstract class ProductBean implements javax.ejb.EntityBean { // EJB callbacks, create methods, other CMP accessors public abstract java.util.Collection getProductItems; public abstract void setProductItems(java.util.Collection newProductItems); public java.util.Collection getItems { Collection prodItems = getProductItems(); java.util.ArrayList items = new ArrayList(prodItems.size()); java.util.Iterator iter = prodItems.iterator(); while (iter.hasNext() { ProductItemLocal prodItemLocal = (ProductItemLocal) iter.next(); items.add(prodItemLocal.getItem()); } return item; } }
The great thing is, you can choose either approach for now, and change it later on without changing the client interface to your bean.