Using CMR field as Primary Keys? Entity without PK possible?


EJB design: Using CMR field as Primary Keys? Entity without PK possible?

  1. Hi,

    I have an entity Product with PK productID. I want to use an entity BillOfMat to describe the bill of material for a product. So I create an entity BillOfMat.
    I know how I'd do it in the DB, but I'm using J2EE RI 1.3.1 with CMP, so I can not create my tables and map the entity fields later. Please correct me, if I'm wrong.
    The BillOfMat entity will only have one attribute 'amount'.
    I want to use CMR to create one relationship between Product and BillOfMat and one for the relationship between BillOfMat and Product, so that I know which Product is contained in which Product. Therefore in BillOfMat I have 2 CMR fields 'masterProduct', 'componentProduct'. So, as I said, amount is the only attribute in BillOfMat and can definetely not be used as a primary key. But I (at least I think) must provide a PK for that entity. But I can only define a PK one a CMP field, not on a CMR field.
    Do I have to use a surrogate (maybe a GUID) for the PK.
    Or can I define CMR fields as PK fields so that I can tell the container that if there are two BillOfMat beans A and B,
    that it is not allowed that A.masterProduct matches B.masterProduct and A.componentProduct matches B.componentProduct.

    Any anyone can, please help.
    TIA Frank
  2. Hi frank,
    We also faced the same kind of problem and came up with a solution to have two columns in the child table (with data replication). One column is defined as a CMP field in the entity bean and the other one as a CMR field. While updating the child bean the same data is paased to both the fields. Hope this wolud help u. If anyone knows any better approch, I would like to hear from u.
  3. Hi,

    but when I have a table with
    masterProductID, componentProductID and amount,
    how can I ensure that there are not to entries
    with the same masterProductID and
    componentProductID respectively.
    I'd like masterProductID and componentProductID to be the primary key for that table.

  4. frank,
    U r right, the PK should be created with the masterProductID and the childProductID. But in the child table u should have one more attribute(column) which will replicate the masterProductID and will act as a CMR field. This is how we have done it.
  5. Hi,

    could you explain that a little more detailed.
    I have to mention that I am using J2EE 1.3 RI,
    so I can not map DB tables to CMP fields.
    Please correct me if that's wrong.
    What do I have to do so that I do not
    the surrogate PK for the relationship