5 Replies Latest reply on Sep 11, 2003 7:58 AM by crazypalm

    CMP Foreign Key problem!

    crazypalm

      Hi, All

      I am a newer of CMP & Jboss. Here I meet a problem when design entitybean relationship.

      I have two entitybean: Transaction & MessageContent whose relationship is 1:1,
      in Transaction CMR:
      /*
      * @ejb.interface-method
      * @ejb.relation
      * name="Transaction-MessageContent"
      * role-name="Transaction-has-MessageContent"
      *
      */
      public abstract MessageContentLocal getMessageContent();
      public abstract void setMessageContent(MessageContentLocal mcontent);

      in MessageContentBean, the CMR is:

      /**
      * @ejb.interface-method
      * @ejb.relation
      * name="Transaction-MessageContent"
      * role-name="MessageContent-belongs_to-Transaction"
      * target-ejb="Transaction"
      * target-role-name="Transaction-has-MessageContent"
      * target-cascade-delete="yes"
      * target-multiple="no"
      *
      * @jboss.relation
      * fk-constraint="true"
      * fk-column="TRANSACTION_ID"
      * related-pk-field="id"
      */
      public abstract TransactionLocal getTransaction();
      public abstract void setTransaction(TransactionLocal transaction);

      the MessageContent.TRANSACTION_ID is foreignkey point to Transaction.ID. I write some test code to popluate some record into these two tables. I though the MessageContent.TRANSACTION_ID must be synchronized with Transaction.ID automatically, but it seems that it's not the truth. So how can I make the foreignkey synchronize with other table's field.

      Anyone could give me a help will be much appreciated!

      Thanks.

        • 1. Re: CMP Foreign Key problem!
          jamarkha

          Show us your client code and the ejbCreate/ejbPostCreate methods for each EJB.

          • 2. Re: CMP Foreign Key problem!
            crazypalm

            BTW, the field MessageContent.TransactionId is null in every trial. :(

            • 3. Re: CMP Foreign Key problem!
              crazypalm

              Nothing special actually in each create();

              in TransactionBean:
              /**
              * @ejb.create-method
              */
              public String ejbCreate(String TransactionId, long accountId ) throws CreateException {
              long currentTime = System.currentTimeMillis();
              Timestamp stamp = new Timestamp(currentTime);
              try {
              this.setId(s_keygen.generateKey().toString());
              this.setTransactionId(vaspTransactionId);
              this.setAccountId(accountId);
              this.setTimeStamp(stamp);
              } catch (Exception e) {
              e.printStackTrace();
              }
              return null;
              }

              in MessageContentBean
              /**
              * @ejb.create-method
              */
              public String ejbCreate(MessageContentData data) throws CreateException
              {
              try
              {
              setId(s_keygen.generateKey().toString());
              data.setId(getId());
              setData(data);
              s_log.info("Generated Key:" + getId());
              }
              catch (Exception e)
              {
              e.printStackTrace();
              }
              return null;
              }

              the Client code just generate MessageContentData and call create. (because Messagecontent.TransactionID is foreignkey, there are no setTransactionId() or getTransactionId(). How can I store the transactionId then?) :(

              • 4. Re: CMP Foreign Key problem!
                jamarkha

                If you want the relationship to be set on ejbCreate(), you need to actually set relationships in a CMP Bean's ejbPostCreate().

                Since the signature for that is the same as ejbCreate, I can see that you do not have as a parameter any reference to a local object for which you want a relationship.

                Example:

                http://xpetstore.sourceforge.net/java2html/xpetstore-ejb/xpetstore/domain/customer/ejb/CustomerEJB.java.html

                • 5. Re: CMP Foreign Key problem!
                  crazypalm

                  Hi,

                  Thanks a lot for your reply.

                  Actually, i read through xpetstore codes again and agian. Then i try modify the create() to ejbCreate(MessageContentLocal mcl, String TransactionId, long accountId ) and ejbCreate(MessageContentLocal mcl, TransactionData tdata), and ejbPostCreate() as well.

                  Then how can I write the clientcode since i dn't know how to get the MessageContentLocal.

                  Also, i found these lines in xpetstore Customer:
                  * @ejb.value-object
                  * compose="xpetstore.domain.signon.model.AccountValue"
                  * compose-name="AccountValue"
                  * members="xpetstore.domain.signon.interfaces.Account"
                  * members-name="AccountValue"
                  * relation="external"

                  what that means? I added these tags in my CMR field, i got compilation error says getMessageContent() not found something.

                  I absolutely have no idea waht to do now.