2 Replies Latest reply on May 8, 2003 5:16 AM by Le Van Huyen

    relation to entity with two primary-key fields

    Le Van Huyen Newbie

      Deer,
      I have 3 tables in my database:
      + 'account' with 4 fields: account_ID*, name, bank_ID, branch_ID
      + 'branch' with 3 fields: branch_ID*, bank_ID*, name
      + 'bank' with 2 fields: bank_ID*, name
      (fields marked with * are PK)
      I want to make three CMP-entities correspond with those tables. Because I need the methods findByBankName and findByBranchName in the account entity, so I used two uni-direction relations: account2branch and branch2bank, both are many2one.
      I use the class BranchPK as PK field of branch entity
      public class BranchPK implements Serializable
      {
      public String branch_id;
      public LocalBank bank_id;
      .....
      }

      But when I create a new account, in my Database, my 'bank_ID' field of 'account' table is empty (my 'branch_ID' is filled with data).
      I want to ask about how to declare primary-key of branch (both in the branchPK class and in ejb-jar.xml file. And how to persist the information of user2branch to user table in database (in ejb-jar.xml and in jbosscmp-jdbc.xml, because I'm using jboss 3.0.6).
      please help me!
      thanhks.

        • 1. Re: relation to entity with two primary-key fields
          Juha Lindfors Master

          well, check the case on field names, there's a mismatch in the example you provided between bank_id and bank_ID... can't see your code so can't tell if the same issue exists there

          • 2. Re: relation to entity with two primary-key fields
            Le Van Huyen Newbie

            Below is my files that I think you will need to help me. if you need other files, please tell me. (I coudldn't attach files, may I email to you?)

            my AccountBean file as follow:

            abstract public class AccountBean implements EntityBean {
            public java.lang.String ejbCreate(java.lang.String account_id, java.lang.String name) throws CreateException {
            setAccount_id(account_id);
            setName(name);
            return null;
            }
            public abstract void setAccount_id(java.lang.String account_id);
            public abstract void setName(java.lang.String name);
            public abstract void setBranch_id(test.LocalBranch branch_id);
            public abstract java.lang.String getAccount_id();
            public abstract java.lang.String getName();
            public abstract test.LocalBranch getBranch_id();
            ......
            }


            my BranchBean file as follow:

            { .....
            public abstract void setBranch_id(java.lang.String branch_id);
            public abstract void setName(java.lang.String name);
            public abstract void setBank_id(test.LocalBank bank_id);
            public abstract java.lang.String getBranch_id();
            public abstract java.lang.String getName();
            public abstract test.LocalBank getBank_id();
            .....
            }



            my BranchPK class is declared as follow

            public class BranchPK implements Serializable {
            public String branch_id;
            public LocalBank bank_id;
            public BranchPK(String branch_id, LocalBank bank_id) {
            this.branch_id = branch_id;
            this.bank_id = bank_id;
            }
            ....
            }


            And, I used a stateless session bean 'Manager' has a method "createMerchant":

            public void createAccount(String account_id, String name, String branch_id, String bank_id) {
            LocalAccount tmpLocal = myAccountHome.create(account_id, name);
            tmpLocal.setBranch_id(myBranchHome.findByPrimaryKey(new BranchPK(branch_id, myBankHome.findByPrimaryKey(bank_id))));
            }


            the ejb-jar.xml is:
            ....

            <display-name>Branch</display-name>
            <ejb-name>Branch</ejb-name>
            <local-home>test.LocalBranchHome</local-home>
            test.LocalBranch
            <ejb-class>test.BranchBean</ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>test.BranchPK</prim-key-class>
            False
            <cmp-version>2.x</cmp-version>
            <abstract-schema-name>Branch</abstract-schema-name>
            <cmp-field>
            <field-name>branch_id</field-name>
            </cmp-field>
            <cmp-field>
            <field-name>name</field-name>
            </cmp-field>


            <display-name>Account</display-name>
            <ejb-name>Account</ejb-name>
            <local-home>test.LocalAccountHome</local-home>
            test.LocalAccount
            <ejb-class>test.AccountBean</ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>java.lang.String</prim-key-class>
            False
            <cmp-version>2.x</cmp-version>
            <abstract-schema-name>Account</abstract-schema-name>
            <cmp-field>
            <field-name>account_id</field-name>
            </cmp-field>
            <cmp-field>
            <field-name>name</field-name>
            </cmp-field>
            <primkey-field>account_id</primkey-field>

            <query-method>
            <method-name>findByBankName</method-name>
            <method-params>
            <method-param>java.lang.String</method-param>
            </method-params>
            </query-method>
            <ejb-ql>SELECT object(o) FROM Account o WHERE o.branch_id.bank_id.name = ?1</ejb-ql>


            </enterprise-beans>

            <ejb-relation>
            <ejb-relation-name>branch-bank</ejb-relation-name>
            <ejb-relationship-role>
            branch
            <ejb-relationship-role-name>BranchRelationshipRole</ejb-relationship-role-name>
            Many
            <relationship-role-source>
            branch
            <ejb-name>Branch</ejb-name>
            </relationship-role-source>
            <cmr-field>
            bank
            <cmr-field-name>bank_id</cmr-field-name>
            </cmr-field>
            </ejb-relationship-role>
            <ejb-relationship-role>
            bank
            <ejb-relationship-role-name>BankRelationshipRole</ejb-relationship-role-name>
            One
            <relationship-role-source>
            bank
            <ejb-name>Bank</ejb-name>
            </relationship-role-source>
            </ejb-relationship-role>
            </ejb-relation>
            <ejb-relation>
            <ejb-relation-name>account-branch</ejb-relation-name>
            <ejb-relationship-role>
            account
            <ejb-relationship-role-name>AccountRelationshipRole</ejb-relationship-role-name>
            Many
            <relationship-role-source>
            account
            <ejb-name>Account</ejb-name>
            </relationship-role-source>
            <cmr-field>
            branch
            <cmr-field-name>branch_id</cmr-field-name>
            </cmr-field>
            </ejb-relationship-role>
            <ejb-relationship-role>
            branch
            <ejb-relationship-role-name>BranchRelationshipRole</ejb-relationship-role-name>
            One
            <relationship-role-source>
            branch
            <ejb-name>Branch</ejb-name>
            </relationship-role-source>
            </ejb-relationship-role>
            </ejb-relation>


            the jbosscmp-jdbc.xml file:
            .....
            <enterprise-beans>

            <ejb-name>Bank</ejb-name>
            <table-name>Bank</table-name>
            <ejb-designer-id>Bank</ejb-designer-id>
            <cmp-field>
            <field-name>bank_id</field-name>
            </cmp-field>
            <cmp-field>
            <field-name>name</field-name>
            </cmp-field>


            <ejb-name>Branch</ejb-name>
            <table-name>Branch</table-name>
            <ejb-designer-id>Branch</ejb-designer-id>
            <cmp-field>
            <field-name>branch_id</field-name>
            </cmp-field>
            <cmp-field>
            <field-name>name</field-name>
            </cmp-field>


            <ejb-name>Account</ejb-name>
            <table-name>Account</table-name>
            <ejb-designer-id>Account</ejb-designer-id>
            <cmp-field>
            <field-name>account_id</field-name>
            </cmp-field>
            <cmp-field>
            <field-name>name</field-name>
            </cmp-field>

            </enterprise-beans>

            <ejb-relation>
            <ejb-relation-name>branch-bank</ejb-relation-name>
            <foreign-key-mapping />
            <ejb-relationship-role>
            <ejb-relationship-role-name>BranchRelationshipRole</ejb-relationship-role-name>
            <ejb-designer-id>Branch</ejb-designer-id>
            </ejb-relationship-role>
            <ejb-relationship-role>
            <ejb-relationship-role-name>BankRelationshipRole</ejb-relationship-role-name>
            </ejb-relationship-role>
            </ejb-relation>
            <ejb-relation>
            <ejb-relation-name>account-branch</ejb-relation-name>
            <foreign-key-mapping />
            <ejb-relationship-role>
            <ejb-relationship-role-name>AccountRelationshipRole</ejb-relationship-role-name>
            <ejb-designer-id>Account</ejb-designer-id>
            </ejb-relationship-role>
            <ejb-relationship-role>
            <ejb-relationship-role-name>BranchRelationshipRole</ejb-relationship-role-name>
            </ejb-relationship-role>
            </ejb-relation>

            ...