Yes, I did find a solution.   The details:

1) For the data model, I specify a single (auto_increment) field for primary 
key and single field for a 'parent' key.  Then I create a 'unique index' on the 
logical key (e.g., productID) and parentKey.  My 'product' table follows:


  | create table if not exists store_product (
  |     productKey      integer not null auto_increment,
  |     parentKey       integer,
  |     productId       varchar (40),
  |     name            varchar (80),
  |     description     varchar (255),
  |     INDEX productKey_indx (productKey),
  |     INDEX productID_indx (productId),
  |     INDEX parentKey_indx (parentKey),
  |     UNIQUE INDEX (parentKey, productId),
  |     PRIMARY KEY (productKey)
  | ) type=InnoDB;
  | 
All other tables in this hierarchy follow the same pattern.

In the ProductBean, I specify a single field as a primary key:

  |     /**
  |      * @ejb.pk-field
  |      * @ejb.persistence
  |      *      column-name="productKey"
  |      * @ejb.interface-method
  |      * @ejb.transaction
  |      *      type="Supports"
  |      * @jboss.persistence
  |      *      auto-increment="true"
  |      */
  |     public abstract java.lang.Integer getProductKey ();
  | 
  |     public abstract void setProductKey (java.lang.Integer ProductKey);
  | 
2)  In the ProductBean, I have a finder method defined to find an entry with 
the appropriate logical key structure:

  |  * @ejb.finder
  |  *     signature="ProductLocal findById (java.lang.String iId, 
java.lang.Integer pKey)"
  |  *     query="SELECT OBJECT(a) FROM Product AS a WHERE (a.productId = ?1) 
AND (a.typeCategory1.typeCatKey = ?2)"
  |  *     view-type="local"
  | 
3) For the relationship with the parent, I specify a single field key, as 
follows:

  |     /**
  |      * @ejb.interface-method
  |      * @ejb.relation
  |      *      name="typeCategory1-products"
  |      *      role-name="product-belongs_to-typeCategory1"
  |      *      cascade-delete="yes"
  |      *
  |      * @jboss.relation
  |      *      fk-column="parentKey"
  |      *      related-pk-field="typeCatKey"
  |      *      fk-contraint="true"
  |      */
  |     public abstract TypeCategory1Local getTypeCategory1 ();
  | 
  |     public abstract void setTypeCategory1 (TypeCategory1Local 
typeCategory1);
  | 
4) in the parent bean (TypeCategory1), I have an 'addChild(ProductValue)' 
business method that adds a Product entry.  The following code is a summary of 
the real code, error checking, etc., removed:

  |     /**
  |      *  @ejb.interface-method
  |      *  Add a child (product entry).
  |      */
  | 
  |     public Integer addChild (ProductValue child)
  |       throws FinderException, CreateException, DuplicateException
  |     {
  |         Integer objKey = null;
  |         ProductLocal oLocal;
  |         ProductLocalHome oHome = ProductUtil.getLocalHome();
  |         boolean dupfound = true;
  |         try {
  |             olocal = oHome.findById(child.getProductId(), getTypeCatKey());
  |         } catch (ObjectNotFoundException OE) {
  |             dupFound = false;
  |         }
  |         if (! dupFound) {
  |             oLocal = oHome.create (child);
  |             getProducts().add (oLocal);
  |             objKey = oLocal.getProductKey();
  |             return objKey;
  |         } else {
  |             throw new DuplicateException();
  |         }
  |     }
  | 
The rest of the beans in the hierarchy follow this same pattern.  

The key to the design here is to separate the logical vs. physical key 
structures.  I use a single field as physical (and bean) primary key, however, 
this pattern allows for any number of fields to be used as a logical key.  I 
just happen to use a single field in this example.  This pattern seems to work 
well, but may be a bit slow in adding records....

You can do more tweaking, like adding Value objects for the many side of a 
relationship to quickly access children (but be very careful here, you don't 
want to read your entire catalog into memory!!), or  use the 
JBoss.relation-read-ahead xdoclet tag to tell the container to read the entire 
child object in one SQL statement, etc.

Hope this helps.

View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3881605#3881605

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3881605


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to