12 Replies Latest reply on Dec 3, 2001 1:37 PM by dsundstrom

    CMP2.0 - CMR - unidirectional one-one mapping

    maku01

      Hi all!

      Please, can you tell me the right settings in the deployment descriptor for CMP 2 - CMR unidirectional one to one mapping. (Which settings do I have to specify in the files ejb-jar.xml, jbosscmp-jdbc.xml, jaws.xml)


      TIA
      Martin

        • 1. Re: CMP2.0 - CMR - unidirectional one-one mapping
          jcordes

          Hi !

          Although I'm not sure which version of JBoss you are using, these are the steps for JBoss3.0.0alpha (I assume that you know how to setup your beans for table, column-names etc,though).

          I have a bean called CatalogStructureEJB with a self-reference (a child-parent-relationship). If that's not your case, you have to change the targets accordingly.

          In ejb-jar.xml:


          ...
          <ejb-relation>
          <ejb-relation-name>CatalogStructure-CatalogStructure</ejb-relation-name>
          <ejb-relationship-role>
          <ejb-relationship-role-name>CatalogStructureHasParent</ejb-relationship-role-name>
          One
          <relationship-role-source>
          <ejb-name>CatalogStructureEJB</ejb-name>
          </relationship-role-source>
          <cmr-field>
          <cmr-field-name>parent</cmr-field-name>
          </cmr-field>
          </ejb-relationship-role>
          <ejb-relationship-role>
          <ejb-relationship-role-name>ParentHasCatalogStructure</ejb-relationship-role-name>
          One
          <relationship-role-source>
          <ejb-name>CatalogStructureEJB</ejb-name>
          </relationship-role-source>
          </ejb-relationship-role>
          </ejb-relation>


          In jbosscmp-jdbc.xml:


          ...
          <ejb-relation>
          <ejb-relation-name>CatalogStructure-CatalogStructure</ejb-relation-name>
          <foreign-key-mapping>
          <ejb-relationship-role>
          <ejb-relationship-role-name>CatalogStructureHasParent</ejb-relationship-role-name>
          <foreign-key-fields>
          <foreign-key-field>
          <field-name>id</field-name>
          <column-name>parent_id</column-name>
          </foreign-key-field>
          </foreign-key-fields>
          </ejb-relationship-role>
          <ejb-relationship-role>
          <ejb-relationship-role-name>ParentHasCatalogStructure</ejb-relationship-role-name>
          <foreign-key-fields/>
          </ejb-relationship-role>
          </foreign-key-mapping>
          </ejb-relation>...


          In jaws.xml :

          Nothing to be done ;-)

          I hope it helps !

          Bye,

          Jochen.

          • 2. Re: CMP2.0 - CMR - unidirectional one-one mapping
            maku01

            Hi!

            Thanks for your attemption. But it doesn't work
            for me.

            I have the two CMP-Beans Film and User
            and I want to have a unidirectional relationship "film-has-owner".

            In the DB the table FILM has the foreign key OWNER
            which points to ID of the table USER.

            I have problems to create a new Film because the id of the film
            is updated with null after the first correct INSERT-STATEMENT.


            The JBoss-Log:
            --------------
            2001-11-09 10:35:20,672 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Create command executing: INSERT INTO Film (id, version, name, description, runtime) VALUES (?, ?, ?, ?, ?)
            2001-11-09 10:35:20,672 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Set parameter: index=1, jdbcType=INTEGER, value=40
            2001-11-09 10:35:20,672 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Set parameter: index=2, jdbcType=INTEGER, value=0
            2001-11-09 10:35:20,672 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Set parameter: index=3, jdbcType=VARCHAR, value=Der mit der Maus tanzt
            2001-11-09 10:35:20,672 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Set parameter: index=4, jdbcType=VARCHAR, value=Actionreisser
            2001-11-09 10:35:20,672 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Set parameter: index=5, jdbcType=INTEGER, value=100
            2001-11-09 10:35:20,682 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Rows affected = 1
            2001-11-09 10:35:20,682 [RMI TCP Connection(4)-10.10.1.183] DEBUG org.jboss.resource.JBossConnectionListenerImpl.DefaultDS - Connection handle 'org.jboss.resource.adapter.jdbc.local.ConnectionInPool@217e67' closed from connection manager 'org.jboss.resource.connectionmanager.jboss.MinervaSharedLocalCM@5c3987' from mcf 'org.jboss.resource.adapter.jdbc.local.JDBCManagedConnectionFactory@71f189'
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG org.jboss.tm.TxCapsule - registerSynchronization(): Entered, tx=XidImpl [FormatId=257, GlobalId=gd2303//3, BranchQual=] status=STATUS_ACTIVE
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG org.jboss.tm.TxCapsule - registerSynchronization(): Entered, tx=XidImpl [FormatId=257, GlobalId=gd2303//3, BranchQual=] status=STATUS_ACTIVE
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG org.jboss.tm.TxCapsule - registerSynchronization(): Entered, tx=XidImpl [FormatId=257, GlobalId=gd2303//3, BranchQual=] status=STATUS_ACTIVE
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG org.jboss.tm.TxManager - suspended tx: TransactionImpl:XidImpl [FormatId=257, GlobalId=gd2303//3, BranchQual=]
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG org.jboss.tm.TxManager - resumed tx: TransactionImpl:XidImpl [FormatId=257, GlobalId=gd2303//3, BranchQual=]
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG org.jboss.tm.TxCapsule - Committing, tx=XidImpl [FormatId=257, GlobalId=gd2303//3, BranchQual=], status=STATUS_ACTIVE
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Store command executing: UPDATE Film SET id=? WHERE id=?
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Set parameter: index=1, jdbcType=INTEGER, value=NULL
            2001-11-09 10:35:20,692 [RMI TCP Connection(4)-10.10.1.183] DEBUG CMP - Set parameter: index=2, jdbcType=INTEGER, value=40



            Tables:

            FILM
            =====
            ID INTEGER
            NAME VARCHAR
            OWNER INTEGER
            ...

            USER
            ====
            ID INTEGER
            NAME VARCHAR
            ...


            ejb-jar.xml
            -----------------

            <!-- Relationships -->

            <ejb-relation>
            <ejb-relation-name>Film-Owner</ejb-relation-name>
            <ejb-relationship-role>
            <ejb-relationship-role-name>film-has-owner</ejb-relationship-role-name>
            One
            <relationship-role-source>
            <ejb-name>Film</ejb-name>
            </relationship-role-source>
            <cmr-field>
            <cmr-field-name>owner</cmr-field-name>
            </cmr-field>
            </ejb-relationship-role>

            <ejb-relationship-role>
            <ejb-relationship-role-name>owner-has-film</ejb-relationship-role-name>
            One
            <relationship-role-source>
            <ejb-name>User</ejb-name>
            </relationship-role-source>
            </ejb-relationship-role>

            </ejb-relation>



            jbosscmp-jdbc.xml
            -----------------
            <jbosscmp-jdbc>

            <!-- Relationships -->

            <ejb-relation>
            <ejb-relation-name>Film-Owner</ejb-relation-name>

            <foreign-key-mapping>
            <ejb-relationship-role>
            <ejb-relationship-role-name>film-has-owner</ejb-relationship-role-name>
            <foreign-key-fields>
            <foreign-key-field>
            <field-name>id</field-name>
            <column-name>id</column-name>
            </foreign-key-field>
            </foreign-key-fields>
            </ejb-relationship-role>

            <ejb-relationship-role>
            <ejb-relationship-role-name>owner-has-film</ejb-relationship-role-name>
            <foreign-key-fields/>
            </ejb-relationship-role>

            </foreign-key-mapping>

            </ejb-relation>


            </jbosscmp-jdbc>


            Please, can you help me?

            TIA
            Martin

            • 3. Re: CMP2.0 - CMR - unidirectional one-one mapping
              jcordes

              Hi Martin !

              Now I see your problem more clearly. You have to set the column-name to a differnt value then id (that's actually the field for the foreign key).

              So your jbosscmp-jdbc.xml file should look like this

              <jbosscmp-jdbc>

              <!-- Relationships -->

              <ejb-relation>
              <ejb-relation-name>Film-Owner</ejb-relation-name>

              <foreign-key-mapping>
              <ejb-relationship-role>
              <ejb-relationship-role-name>film-has-owner</ejb-relationship-role-name>
              <foreign-key-fields>
              <foreign-key-field>
              <field-name>id</field-name>
              <column-name>owner_id</column-name>
              </foreign-key-field>
              </foreign-key-fields>
              </ejb-relationship-role>

              <ejb-relationship-role>
              <ejb-relationship-role-name>owner-has-film</ejb-relationship-role-name>
              <foreign-key-fields/>
              </ejb-relationship-role>

              </foreign-key-mapping>

              </ejb-relation>


              </jbosscmp-jdbc>


              Good luck !

              Jochen.


              • 4. Re: CMP2.0 - CMR - unidirectional one-one mapping
                maku01

                Hi Jochen!

                Thank you very much. Now it works...

                Greetings!

                Martin

                • 5. Re: CMP2.0 - CMR - unidirectional one-one mapping
                  bchi49

                  Is it a requirement to use jbosscmp-jdbc.xml file to declare relationships?

                  I'm using JBoss 3 Alpha with MySQL 3.23.44 max. And I'm getting a null pointer exception on line 09 and 12 of the following: I created two Entity beans with a one-many relationships to the 2nd Entity bean(e.x. Recipe has many Ingredient).

                  01:InitialContext ic = new InitialContext();
                  02:obj = ic.lookup("RecipeBean");
                  03:RecipeHome dbh = (RecipeHome) PortableRemoteObject.narrow(obj, RecipeHome.class);
                  04:Recipe recipe = dbh.create();
                  05:recipe.setName("Recipe Name");
                  06:obj = ic.lookup("IngredientBean");
                  07:IngredientHome ibh = (IngredientHome) PortableRemoteObject.narrow(obj, IngredientHome.class);
                  08:Ingredient ingredient = ibh.create();
                  09:ingredient.setRecipe( recipe );
                  10:Set IngredientSet = new HashSet();
                  11:IngredientSet.add( ingredient );
                  12:recipe.setIngredients( IngredientSet );

                  The ejb-jar.xml is:
                  <?xml version="1.0"?>
                  <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd'>
                  <ejb-jar>
                      
                          <ejb-relation>
                              
                              <ejb-relation-name>Recipe-Ingredients</ejb-relation-name>
                              <ejb-relationship-role>
                                  <ejb-relationship-role-name>IngredientBean-belongsto-RecipeBean</ejb-relationship-role-name>
                                  Many
                                  <relationship-role-source>
                                      <ejb-name>IngredientBean</ejb-name>
                                  </relationship-role-source>
                                  <cmr-field>
                                      <cmr-field-name>recipe</cmr-field-name>
                                  </cmr-field>
                              </ejb-relationship-role>
                              <ejb-relationship-role>
                                  <ejb-relationship-role-name>RecipeBean-Has-IngredientBean</ejb-relationship-role-name>
                                  One
                                  <relationship-role-source>
                                      <ejb-name>RecipeBean</ejb-name>
                                  </relationship-role-source>
                                  <cmr-field>
                                      <cmr-field-name>ingredients</cmr-field-name>
                                      <cmr-field-type>java.util.Collection</cmr-field-type>
                                  </cmr-field>
                              </ejb-relationship-role>
                          </ejb-relation>
                      
                      <assembly-descriptor>...
                  </ejb-jar>


                  The exception was:
                  23:10:39,825 ERROR [Default] java.lang.ClassCastException: $Proxy19
                  23:10:39,825 ERROR [Default] at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.setValue(JDBCCMRFieldBridge.java:446)
                  23:10:39,835 ERROR [Default] at org.jboss.ejb.plugins.cmp.bridge.EntityBridgeInvocationHandler.invoke(EntityBridgeInvocationHandler.java:119)
                  23:10:39,835 ERROR [Default] at org.jboss.proxy.ProxyCompiler$Runtime.invoke(ProxyCompiler.java:89)
                  23:10:39,835 ERROR [Default] at com.d2g.oo.foodnetwork.dish.ejb.IngredientBean$Proxy.setRecipe(Unknown Source)
                  23:10:39,845 ERROR [Default] at java.lang.reflect.Method.invoke(Native Method)
                  23:10:39,845 ERROR [Default] at org.jboss.ejb.EntityContainer$ContainerInterceptor.invoke(EntityContainer.java:1002)
                  23:10:39,845 ERROR [Default] at org.jboss.ejb.plugins.cmp.jdbc.JDBCReadAheadInterceptor.invoke(JDBCReadAheadInterceptor.java:100)
                  23:10:39,845 ERROR [Default] at org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor.invoke(JDBCRelationInterceptor.java:134)
                  23:10:39,855 ERROR [Default] at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke(EntitySynchronizationInterceptor.java:308)
                  23:10:39,855 ERROR [Default] at org.jboss.ejb.plugins.EntityInstanceInterceptor.invoke(EntityInstanceInterceptor.java:201)
                  23:10:39,855 ERROR [Default] at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLockInterceptor.java:132)
                  23:10:39,865 ERROR [Default] at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:98)
                  23:10:39,865 ERROR [Default] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:156)
                  23:10:39,865 ERROR [Default] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:65)
                  23:10:39,865 ERROR [Default] at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:131)
                  23:10:39,875 ERROR [Default] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:166)
                  23:10:39,875 ERROR [Default] at org.jboss.ejb.EntityContainer.invoke(EntityContainer.java:447)
                  23:10:39,875 ERROR [Default] at org.jboss.ejb.Container.invoke(Container.java:528)
                  23:10:39,875 ERROR [Default] at com.sun.management.jmx.MBeanServerImpl.invoke(MBeanServerImpl.java:1555)
                  23:10:39,875 ERROR [Default] at com.sun.management.jmx.MBeanServerImpl.invoke(MBeanServerImpl.java:1523)
                  23:10:39,885 ERROR [Default] at org.jboss.ejb.plugins.jrmp.server.JRMPContainerInvoker.invoke(JRMPContainerInvoker.java:411)
                  23:10:39,885 ERROR [Default] at java.lang.reflect.Method.invoke(Native Method)
                  23:10:39,885 ERROR [Default] at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:241)
                  23:10:39,885 ERROR [Default] at sun.rmi.transport.Transport$1.run(Transport.java:152)
                  23:10:39,885 ERROR [Default] at java.security.AccessController.doPrivileged(Native Method)
                  23:10:39,895 ERROR [Default] at sun.rmi.transport.Transport.serviceCall(Transport.java:148)
                  23:10:39,895 ERROR [Default] at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:465)
                  23:10:39,895 ERROR [Default] at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:706)
                  23:10:39,895 ERROR [Default] at java.lang.Thread.run(Thread.java:484)
                  23:10:39,916 ERROR [EntityContainer] invoke returned an exception

                  looking at the exception and the JBoss source code, it's casting my Recipe EntityBean to EJBLocalObject, that's where the exception occurs. It's casting to EJBLocalObject to get the PrimaryKey of the entity bean.

                  Maybe I shouldn't be passing in the Recipe bean to the "09:ingredient.setRecipe( recipe );"

                  Am I doing something wrong?

                  thanks,
                  Benjamin

                  • 6. Re: CMP2.0 - CMR - unidirectional one-one mapping
                    jcordes

                    Hi Benjamin !

                    Are you really using Remote-Interfaces ? I can only guess it, because you're using PortableRemoteObject.narrow(). But there's an error in your configuration. You wanted to use HashSet for your ingredients, but you specified java.util.Collection as cmr-field-type. Maybe the ClassCastException results in this mismatching specification.

                    Bye,

                    Jochen.

                    • 7. Re: CMP2.0 - CMR - unidirectional one-one mapping
                      lafr

                      You must use Local-Objects for cmr-related references.
                      So PortableRemoteObject.narrow() is now longer needed.

                      Why do your Beans have no Primary Key ?

                      • 8. Re: CMP2.0 - CMR - unidirectional one-one mapping
                        bchi49

                        Hi,

                        Yes, I'm using Remote-interfaces. I removed the use of PortableRemoteObject.narrow() and changes the cmr-field-type to use java.util.Set for ingredients because it's only can be a Set or Collection. The ClassCastException still occurs. Like what lafr says, I think I'll need to use Local-Interfaces for doing relationships, it looks like JBoss is accepting LocalInterface for now.
                        Thanks!

                        • 9. Re: CMP2.0 - CMR - unidirectional one-one mapping
                          bchi49

                          Hi,

                          I'm not passing in the primary key to the ejbCreate(). I'm retrieving a unique key within the ejbCreate(). I created a EJBbean that keeps track of tablename and the primary key id, the key is incremented for each call to get the nextKeyId. I found that's slow this way and like to use GUID key generator, but it works for now. :)

                          I removed the PortableRemoteObject.narrow(), but it's still not working. Do you have some references for creating local interfaces?

                          thanks,
                          Benjamin

                          • 10. Re: CMP2.0 - CMR - unidirectional one-one mapping
                            bchi49

                            I see, in the EJB 2.0 spec, it's a requirement to implement local interfaces for Entitybean Relationships. I also found a tutorial at the SUN's site http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/CMP.html that has example of using EJB Relationship and using Local Interfaces.
                            Thanks!

                            • 11. Re: CMP2.0 - CMR - unidirectional one-one mapping
                              stica76

                              Hello I'd use the CMP 2.0 with Jboss3.0 alpha.
                              Can u say me how can I write the jbosscmp-jdbc.xml file? I don't find documentation about it!!!
                              Please help me.
                              Can u give me an example of CMP2.0?
                              Thanks

                              • 12. Re: CMP2.0 - CMR - unidirectional one-one mapping
                                dsundstrom

                                Configuration of JBossCMP, the jbosscmp-jdbc.xml file, is covered in the JBossCMP workbook.

                                -dain