1 Reply Latest reply on Apr 5, 2004 7:20 AM by beal91

    CMR, three way relationship

    glazed

      Hello

      I assume that what I am going to ask cannot be done with the current CMR spec. But I will give the question a go anyway just to be sure.

      I have an existing datamodel that looks like this:

      table1: PRODUCT
      PRODUCT_ID (pk)

      table2: ITEM
      ITEM_ID (pk)

      table3: PRODUCT_ITEM
      PRODUCT_ID (fk PRODUCT.PRODUCT_ID)
      ITEM_ID (fk ITEM.ITEM_ID)
      TYPE (fk PRTYPE.TYPE)

      table4: PRTYPE
      TYPE (pk) (TYPE can be e.g. "BUNDLE", "PACKAGE" etc.)

      So.. Its kind of a relationship between PRODUCT and ITEM where there is some extra info on the relationship. The relationship can be of a specified type.
      Can I create a CMR between PRODUCT and ITEM that can handle these requirements?

      thanks,
      Thomas

        • 1. Re: CMR, three way relationship
          beal91

          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.