5 Replies Latest reply on Apr 3, 2004 5:59 AM by aloubyansky

    Compound PK problem

    javri28

      Hi guys,

      I have a Table wich has 3 columns. They all together are PK for that Table(DB2). But each of them are foreign keys as well.
      So, I need PK class to cope with that. But the question is how to do it -
      I did it but there are problems when creating new bean.
      Here is the part of code:

      public class SasAssignmentPK implements Serializable {
      public Integer sampTableId;
      public Integer productGroupId;
      public Integer sasId;

      public SasAssignmentPK(Integer sampTableId, Integer productGroupId, Integer sasId) {
      this.sampTableId = sampTableId;
      this.productGroupId = productGroupId;
      this.sasId = sasId;
      }

      public boolean equals(Object obj) {
      if (this == obj) {
      return true;
      }
      if (!(obj instanceof SasAssignmentPK)) {
      return false;
      }
      SasAssignmentPK that = (SasAssignmentPK) obj;
      if (!(that.sampTableId == null ? this.sampTableId == null : that.sampTableId.equals(this.sampTableId))) {
      return false;
      }
      if (!(that.productGroupId == null
      ? this.productGroupId == null
      : that.productGroupId.equals(this.productGroupId))) {
      return false;
      }
      if (!(that.sasId == null ? this.sasId == null : that.sasId.equals(this.sasId))) {
      return false;
      }
      return true;
      }

      public int hashCode() {
      int result = 0;
      result = result + this.sampTableId.hashCode();
      result = result + this.productGroupId.hashCode();
      result = result + this.sasId.hashCode();
      return result;
      }

      }


      A have fields with same names in Bean.

      So, the question is how to organize create method.
      Any ideas?

        • 1. Re: Compound PK problem
          beal91

          I would have a create() method that accepts as parameters the three entities that comprise the three-way relationship:

          public SasAssignmentPK ejbCreate(SampTableLocal table,
          ProductGroupLocal prodGroup,
          SasLocal sas) throws javax.ejb.CreateException {
          setSampTableId(table.getTableId());
          setProductGroupId(prodGroup.getGroupId());
          setSasId(sas.getSasId());
          return null;
          }
          I would also define equivalent CMR fields (taking care to map them to the same database columns) for each of these relations, and set the relationship values in a postCreate() method:

          public void ejbPostCreate(SampTableLocal table,
          ProductGroupLocal prodGroup,
          SasLocal sas) {
          setTable(table);
          setProductGroup(prodGroup);
          setSas(sas);
          }
          -- Jeff Beal

          • 2. Re: Compound PK problem

            Why do you set the relations in the post create if you have already set the ids in the create?

            • 3. Re: Compound PK problem
              beal91

              I like to set the relationship field in the code rather than "counting on" the bean deployer to correctly map the two EJB fields to the same column in the database. It's probably not necessary, but it does make the EJB less sensitive to a particular deployment. If I ever decide to deploy my beans in a different application server or to a different database, I don't have to map the database fields in order for the code to work.

              • 4. Re: Compound PK problem
                javri28

                This is what i had in my bean:

                ** @ejb.persistence column-name = "ST_ID"
                * @ejb.interface-method view-type = "both"
                * @return
                */
                abstract public Integer getSampTableId();
                /** @ejb.interface-method view-type = "both" */
                abstract public void setSampTableId(Integer sampTableId);


                /** @ejb.persistence column-name = "PG_ID"
                * @ejb.interface-method view-type = "both"
                * @return
                */
                abstract public Integer getProductGroupId();
                /** @ejb.interface-method view-type = "both" */
                abstract public void setProductGroupId(Integer productGroupId);


                /** @ejb.persistence column-name = "SAS_ID"
                * @ejb.interface-method view-type = "both"
                * @return
                */
                abstract public Integer getSasId();
                /** @ejb.interface-method view-type = "both" */
                abstract public void setSasId(Integer sasId);


                public SasAssignmentPK ejbCreate(SampTableLocal sampTable, ProductGroupLocal productGroup, SampAttributeSetLocal sas) throws CreateException {
                setProductGroupId(productGroup.getId());
                setSasId(sas.getId());
                setSampTableId(sampTable.getId());
                return null;
                }


                public void ejbPostCreate(SampTableLocal sampTable, ProductGroupLocal productGroup, SampAttributeSetLocal sas) throws CreateException {
                /*
                setSampTable(sampTable);
                setProductGroup(productGroup);
                setSampAttributeSet(sas);
                */
                }


                And the exception was:

                javax.ejb.TransactionRolledbackLocalException: Internal error setting instance field sampTableId; CausedByException is:
                de.keywork.moc.sampling.pk.SasAssignmentPK; CausedByException is:
                Internal error setting instance field sampTableId; CausedByException is:
                de.keywork.moc.sampling.pk.SasAssignmentPK
                at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:204)
                ... bla-bla-bla


                Any ides after reading this long story? :-)

                • 5. Re: Compound PK problem
                  aloubyansky

                  I guess this is because you don't have a no-arg constructor for the PK class.