3 Replies Latest reply on Oct 14, 2002 1:52 AM by Amy

    cmp2 one to many relation problem

    Amy Newbie

      I'm using jboss-3.0.0_tomcat-4.0.3 in win2000.
      There are two tables named company and contact,the relation between them are one to many.

      Here is the jbosscmp-jdbc.xml:
      <?xml version="1.0" encoding="UTF-8"?>

      <jbosscmp-jdbc>

      java:/OracleDS
      <datasource-mapping>Oracle8</datasource-mapping>

      <enterprise-beans>

      <ejb-name>Contact</ejb-name>
      <table-name>CONTACT</table-name>
      <ejb-designer-id>Contact</ejb-designer-id>
      <cmp-field>
      <field-name>contactid</field-name>
      <column-name>CONTACTID</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>companyid</field-name>
      <column-name>COMPANYID</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>firstname</field-name>
      <column-name>FIRSTNAME</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>midname</field-name>
      <column-name>MIDNAME</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>lastname</field-name>
      <column-name>LASTNAME</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>sex</field-name>
      <column-name>SEX</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>title</field-name>
      <column-name>TITLE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>workphone</field-name>
      <column-name>WORKPHONE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>cellphone</field-name>
      <column-name>CELLPHONE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>homephone</field-name>
      <column-name>HOMEPHONE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>fax</field-name>
      <column-name>FAX</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>email</field-name>
      <column-name>EMAIL</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>password</field-name>
      <column-name>PASSWORD</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>role</field-name>
      <column-name>ROLE</column-name>
      </cmp-field>


      <ejb-name>Company</ejb-name>
      <table-name>COMPANY</table-name>
      <ejb-designer-id>Company</ejb-designer-id>
      <cmp-field>
      <field-name>companyid</field-name>
      <column-name>COMPANYID</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>companyname</field-name>
      <column-name>COMPANYNAME</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>type</field-name>
      <column-name>TYPE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>taxid</field-name>
      <column-name>TAXID</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>status</field-name>
      <column-name>STATUS</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>rank</field-name>
      <column-name>RANK</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>phone</field-name>
      <column-name>PHONE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>fax</field-name>
      <column-name>FAX</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>website</field-name>
      <column-name>WEBSITE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>tradecurrency</field-name>
      <column-name>TRADECURRENCY</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>parentid</field-name>
      <column-name>PARENTID</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>invaliddate</field-name>
      <column-name>INVALIDDATE</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>password</field-name>
      <column-name>PASSWORD</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>bank</field-name>
      <column-name>BANK</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>accounts</field-name>
      <column-name>ACCOUNTS</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>credit</field-name>
      <column-name>CREDIT</column-name>
      </cmp-field>

      </enterprise-beans>

      <ejb-relation>
      <ejb-relation-name>company-contact</ejb-relation-name>
      <foreign-key-mapping />
      <ejb-relationship-role>
      <ejb-relationship-role-name>CompanyContactRelationshipRole</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>companyid</field-name>
      <column-name>COMPANYID</column-name>
      </key-field>
      </key-fields>
      <ejb-designer-id>Company</ejb-designer-id>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>ContactCompanyRelationshipRole</ejb-relationship-role-name>
      </ejb-relationship-role>
      </ejb-relation>

      </jbosscmp-jdbc>

      ejb-jar.xml:
      <ejb-jar>
      <enterprise-beans>

      <display-name>Contact</display-name>
      <ejb-name>Contact</ejb-name>
      <local-home>com.eview.scm.components.company.contact.ejb.ContactLocalHome</local-home>
      com.eview.scm.components.company.contact.ejb.ContactLocal
      <ejb-class>com.eview.scm.components.company.contact.ejb.ContactBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      False
      <abstract-schema-name>Contact</abstract-schema-name>
      <cmp-field>
      <field-name>contactid</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>companyid</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>firstname</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>midname</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>lastname</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>sex</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>title</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>workphone</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>cellphone</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>homephone</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>fax</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>email</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>password</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>role</field-name>
      </cmp-field>
      <primkey-field>contactid</primkey-field>


      <display-name>Company</display-name>
      <ejb-name>Company</ejb-name>
      <local-home>com.eview.scm.components.company.ejb.CompanyLocalHome</local-home>
      com.eview.scm.components.company.ejb.CompanyLocal
      <ejb-class>com.eview.scm.components.company.ejb.CompanyBean</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>Company</abstract-schema-name>
      <cmp-field>
      <field-name>companyid</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>companyname</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>type</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>taxid</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>status</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>rank</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>phone</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>fax</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>website</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>tradecurrency</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>parentid</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>invaliddate</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>password</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>bank</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>accounts</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>credit</field-name>
      </cmp-field>
      <primkey-field>companyid</primkey-field>
      <ejb-local-ref>

      <ejb-ref-name>ejb/local/Contact</ejb-ref-name>
      <ejb-ref-type>Entity</ejb-ref-type>
      <local-home>com.eview.scm.components.company.contact.ejb.ContactLocalHome</local-home>
      com.eview.scm.components.company.contact.ejb.ContactLocal
      <ejb-link>Contact</ejb-link>
      </ejb-local-ref>

      </enterprise-beans>

      <ejb-relation>
      <ejb-relation-name>company-contact</ejb-relation-name>
      <ejb-relationship-role>
      company
      <ejb-relationship-role-name>CompanyContactRelationshipRole</ejb-relationship-role-name>
      One
      <relationship-role-source>
      company
      <ejb-name>Company</ejb-name>
      </relationship-role-source>
      <cmr-field>
      contact
      <cmr-field-name>contact</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      contact
      <ejb-relationship-role-name>ContactCompanyRelationshipRole</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      contact
      <ejb-name>Contact</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>

      <assembly-descriptor>
      <container-transaction>

      <ejb-name>Contact</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>Company</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>

      And my question is:
      Can I use the code list below to insert into company and contact in one transaction?

      abstract public class CompanyBean implements EntityBean {
      EntityContext entityContext;

      public java.lang.String ejbCreate(java.lang.String companyid, java.lang.String companyname, java.lang.String type, java.lang.String taxid, java.lang.String status, java.lang.String rank, java.lang.String phone, java.lang.String fax, java.lang.String website, java.lang.String tradecurrency, java.lang.String parentid, java.lang.String invaliddate, java.lang.String password, java.lang.String bank, java.lang.String accounts, java.math.BigDecimal credit, java.util.Collection contact) throws CreateException {
      setCompanyid(companyid);
      setCompanyname(companyname);
      setType(type);
      setTaxid(taxid);
      setStatus(status);
      setRank(rank);
      setPhone(phone);
      setFax(fax);
      setWebsite(website);
      setTradecurrency(tradecurrency);
      setParentid(parentid);
      setInvaliddate(invaliddate);
      setPassword(password);
      setBank(bank);
      setAccounts(accounts);
      setCredit(credit);
      return null;
      }
      public void ejbPostCreate(java.lang.String companyid, java.lang.String companyname, java.lang.String type, java.lang.String taxid, java.lang.String status, java.lang.String rank, java.lang.String phone, java.lang.String fax, java.lang.String website, java.lang.String tradecurrency, java.lang.String parentid, java.lang.String invaliddate, java.lang.String password, java.lang.String bank, java.lang.String accounts, java.math.BigDecimal credit, java.util.Collection contact) throws CreateException {
      this.setContact(contact);
      }

      public void ejbRemove() throws RemoveException {
      /**@todo Complete this method*/
      }
      public abstract void setCompanyid(java.lang.String companyid);
      public abstract void setCompanyname(java.lang.String companyname);
      public abstract void setType(java.lang.String type);
      public abstract void setTaxid(java.lang.String taxid);
      public abstract void setStatus(java.lang.String status);
      public abstract void setRank(java.lang.String rank);
      public abstract void setPhone(java.lang.String phone);
      public abstract void setFax(java.lang.String fax);
      public abstract void setWebsite(java.lang.String website);
      public abstract void setTradecurrency(java.lang.String tradecurrency);
      public abstract void setParentid(java.lang.String parentid);
      public abstract void setInvaliddate(java.lang.String invaliddate);
      public abstract void setPassword(java.lang.String password);
      public abstract void setBank(java.lang.String bank);
      public abstract void setAccounts(java.lang.String accounts);
      public abstract void setCredit(java.math.BigDecimal credit);
      public abstract void setContact(java.util.Collection contact);
      public abstract java.lang.String getCompanyid();
      public abstract java.lang.String getCompanyname();
      public abstract java.lang.String getType();
      public abstract java.lang.String getTaxid();
      public abstract java.lang.String getStatus();
      public abstract java.lang.String getRank();
      public abstract java.lang.String getPhone();
      public abstract java.lang.String getFax();
      public abstract java.lang.String getWebsite();
      public abstract java.lang.String getTradecurrency();
      public abstract java.lang.String getParentid();
      public abstract java.lang.String getInvaliddate();
      public abstract java.lang.String getPassword();
      public abstract java.lang.String getBank();
      public abstract java.lang.String getAccounts();
      public abstract java.math.BigDecimal getCredit();
      public abstract java.util.Collection getContact();
      public void ejbLoad() {
      /**@todo Complete this method*/
      }
      public void ejbStore() {
      /**@todo Complete this method*/
      }
      public void ejbActivate() {
      /**@todo Complete this method*/
      }
      public void ejbPassivate() {
      /**@todo Complete this method*/
      }
      public void unsetEntityContext() {
      this.entityContext = null;
      }
      public void setEntityContext(EntityContext entityContext)
      {
      this.entityContext = entityContext;
      }
      }

        • 1. Re: cmp2 one to many relation problem
          Alexey Loubyansky Master

          Yes. Also you can use value object pattern to pass parameters.

          • 2. Re: cmp2 one to many relation problem
            Amy Newbie

            Thank loubyansky!

            I meet another problem when I pass parameters with value object.
            I pass an ArrayList named contact, which is filled with some dbTableModels . Class dbTableModel describes the struct of table contact.
            The log shows these error:
            09:38:49,537 ERROR [LogInterceptor] TransactionRolledbackException, causedBy:java.lang.ClassCastException: com.eview.scm.util.dbTableModel at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.setInstanceV
            alue(JDBCCMRFieldBridge.java:624)
            at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.setValue(JDBCCMRFieldBridge.java:563)
            at org.jboss.ejb.plugins.cmp.bridge.EntityBridgeInvocationHandler.invoke
            (EntityBridgeInvocationHandler.java:125)
            at org.jboss.proxy.compiler.Runtime.invoke(Runtime.java:59)
            at com.eview.scm.components.company.ejb.CompanyBean$Proxy.setAddress(<ge
            nerated>)
            at com.eview.scm.components.company.ejb.CompanyBean.ejbPostCreate(Compan
            yBean.java:75)
            at java.lang.reflect.Method.invoke(Native Method)
            at org.jboss.ejb.plugins.CMPPersistenceManager.postCreateEntity(CMPPersi
            stenceManager.java:284)
            at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.post
            CreateEntity(CachedConnectionInterceptor.java:278)
            at org.jboss.ejb.EntityContainer.postCreateLocalHome(EntityContainer.jav
            a:593)
            at java.lang.reflect.Method.invoke(Native Method)
            at org.jboss.ejb.EntityContainer$ContainerInterceptor.invoke(EntityConta
            iner.java:1174)
            at org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor.invoke(JDBCRelationInterceptor.java:190)
            at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke(EntityS
            ynchronizationInterceptor.java:340)
            at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
            at org.jboss.ejb.plugins.EntityInstanceInterceptor.invoke(EntityInstanceInterceptor.java:193)
            at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLockInterceptor.java:107)
            at org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCreationInterceptor.java:59)
            at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:98)
            at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:167)
            at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:52)
            at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:104)
            at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:109)
            at org.jboss.ejb.EntityContainer.invokeHome(EntityContainer.java:487)
            at org.jboss.ejb.plugins.local.BaseLocalContainerInvoker.invokeHome(BaseLocalContainerInvoker.java:227)
            at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:110)
            at $Proxy52.create(Unknown Source)
            at com.eview.scm.components.company.ejb.CompanySessionBean.createCompany(CompanySessionBean.java:84)
            at java.lang.reflect.Method.invoke(Native Method)
            at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:664)
            at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
            at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:77)
            at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:96)
            at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:167)
            at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:61)
            at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:129)
            at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:166)
            at org.jboss.ejb.StatelessSessionContainer.invoke(StatelessSessionContainer.java:313)
            at org.jboss.ejb.Container.invoke(Container.java:705)
            at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:491)
            at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:362)
            at java.lang.reflect.Method.invoke(Native Method)
            at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:241)
            at sun.rmi.transport.Transport$1.run(Transport.java:152)
            at java.security.AccessController.doPrivileged(Native Method)
            at sun.rmi.transport.Transport.serviceCall(Transport.java:148)
            at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:465)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:706)
            at java.lang.Thread.run(Thread.java:484)

            As you will see, I only implemented one ejbcreate method in ContactBean.
            abstract public class ContactBean implements EntityBean {
            EntityContext entityContext;
            public java.lang.String ejbCreate(java.lang.String contactid, java.lang.String companyid, java.lang.String firstname, java.lang.String midname, java.lang.String lastname, java.lang.String sex, java.lang.String title, java.lang.String workphone, java.lang.String cellphone, java.lang.String homephone, java.lang.String fax, java.lang.String email, java.lang.String password, java.lang.String role) throws CreateException {
            setContactid(contactid);
            setCompanyid(companyid);
            setFirstname(firstname);
            setMidname(midname);
            setLastname(lastname);
            setSex(sex);
            setTitle(title);
            setWorkphone(workphone);
            setCellphone(cellphone);
            setHomephone(homephone);
            setFax(fax);
            setEmail(email);
            setPassword(password);
            setRole(role);
            return null;
            }
            public void ejbPostCreate(java.lang.String contactid, java.lang.String companyid, java.lang.String firstname, java.lang.String midname, java.lang.String lastname, java.lang.String sex, java.lang.String title, java.lang.String workphone, java.lang.String cellphone, java.lang.String homephone, java.lang.String fax, java.lang.String email, java.lang.String password, java.lang.String role) throws CreateException {
            /**@todo Complete this method*/
            }
            public void ejbRemove() throws RemoveException {
            /**@todo Complete this method*/
            }
            public abstract void setContactid(java.lang.String contactid);
            public abstract void setCompanyid(java.lang.String companyid);
            public abstract void setFirstname(java.lang.String firstname);
            public abstract void setMidname(java.lang.String midname);
            public abstract void setLastname(java.lang.String lastname);
            public abstract void setSex(java.lang.String sex);
            public abstract void setTitle(java.lang.String title);
            public abstract void setWorkphone(java.lang.String workphone);
            public abstract void setCellphone(java.lang.String cellphone);
            public abstract void setHomephone(java.lang.String homephone);
            public abstract void setFax(java.lang.String fax);
            public abstract void setEmail(java.lang.String email);
            public abstract void setPassword(java.lang.String password);
            public abstract void setRole(java.lang.String role);
            public abstract java.lang.String getContactid();
            public abstract java.lang.String getCompanyid();
            public abstract java.lang.String getFirstname();
            public abstract java.lang.String getMidname();
            public abstract java.lang.String getLastname();
            public abstract java.lang.String getSex();
            public abstract java.lang.String getTitle();
            public abstract java.lang.String getWorkphone();
            public abstract java.lang.String getCellphone();
            public abstract java.lang.String getHomephone();
            public abstract java.lang.String getFax();
            public abstract java.lang.String getEmail();
            public abstract java.lang.String getPassword();
            public abstract java.lang.String getRole();
            public void ejbLoad() {
            /**@todo Complete this method*/
            }
            public void ejbStore() {
            /**@todo Complete this method*/
            }
            public void ejbActivate() {
            /**@todo Complete this method*/
            }
            public void ejbPassivate() {
            /**@todo Complete this method*/
            }
            public void unsetEntityContext() {
            this.entityContext = null;
            }
            public void setEntityContext(EntityContext entityContext) {
            this.entityContext = entityContext;
            }
            }
            Should I implement ejbcreate method with parameter dbTableModel?

            • 3. Re: cmp2 one to many relation problem
              Amy Newbie

              I download the sourcecode of Jboss, and find the solution.
              Thanks everybody!