6 Replies Latest reply on Sep 6, 2002 1:19 PM by Devon Coleman

    CMP Relationship Problem

    Devon Coleman Newbie

      Hello All:

      I am having a problem properly defining relationships. I am using CMP2.0 with JBOSS 3.0 with a mysql db. There are two entity beans (CustomerEJB and AddressEJB)each using a Defined Value Class to populate it non-primary key fields.
      There is a one to one unidirectional relationship between the EJB (a Customer and have a Address). In this relationship the "address_id" column in the Customer table is the foreign key for the "id" column in the Address table. In this case creating a CustomerEJB will create an AddressEJB(which is an extension of the
      EJBLocalObject). When I run the client application the error detailed below occurs. It appears that the address_id column in the Customer table is unable to be updated with the id column from the Address table. All other columns in both tables are properly set though. My question is what am I doing wrong.

      The error message:

      2002-09-05 19:10:12,170 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CustomerEJB] Create: pk=1
      2002-09-05 19:10:12,170 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CustomerEJB] Executing SQL: SELECT COUNT(*) FROM Customer WHERE ID=?
      2002-09-05 19:10:43,430 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CustomerEJB] Create: pk=1
      2002-09-05 19:10:43,430 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CustomerEJB] Executing SQL: SELECT COUNT(*) FROM Customer WHERE ID=?
      2002-09-05 19:10:43,430 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CustomerEJB] Executing SQL: INSERT INTO Customer (ID, last_name, first_name, has_good_credit, address_id) VALUES (?, ?, ?, ?, ?)
      2002-09-05 19:10:43,430 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CustomerEJB] Rows affected = 1
      2002-09-05 19:10:43,650 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCLoadEntityCommand.CustomerEJB] Executing SQL: SELECT last_name, first_name, has_good_credit, address_id FROM Customer WHERE (ID=?)
      2002-09-05 19:10:43,700 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreEntityCommand.CustomerEJB] Executing SQL: UPDATE Customer SET last_name=?, first_name=? WHERE ID=?
      2002-09-05 19:10:43,700 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreEntityCommand.CustomerEJB] Rows affected = 1
      2002-09-05 19:10:43,760 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCLoadEntityCommand.CustomerEJB] Executing SQL: SELECT last_name, first_name, has_good_credit, address_id FROM Customer WHERE (ID=?)
      2002-09-05 19:10:43,810 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.AddressEJB] Create: pk=null
      2002-09-05 19:10:43,810 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.AddressEJB] Executing SQL: SELECT COUNT(*) FROM Address WHERE ID=?
      2002-09-05 19:10:43,810 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.AddressEJB] Executing SQL: INSERT INTO Address (ID, street, city, state, zip) VALUES (?, ?, ?, ?, ?)
      2002-09-05 19:10:43,870 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.AddressEJB] Rows affected = 1
      2002-09-05 19:10:43,920 ERROR [org.jboss.ejb.plugins.LogInterceptor] TransactionRolledbackException, causedBy:
      java.lang.IllegalStateException: removing bean lock and it has tx set!

      at org.jboss.ejb.plugins.lock.QueuedPessimisticEJBLock.removeRef(QueuedPessimisticEJBLock.java:469)
      at org.jboss.ejb.BeanLockManager.removeLockRef(BeanLockManager.java:78)
      at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLockInterceptor.java:124)
      at org.jboss.ejb.plugins.EntityCreationInterceptor.invoke(EntityCreationInterceptor.java:69)
      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.EntityContainer.invoke(EntityContainer.java:493)
      at org.jboss.ejb.plugins.local.BaseLocalContainerInvoker.invoke(BaseLocalContainerInvoker.java:296)
      at org.jboss.ejb.plugins.local.EntityProxy.invoke(EntityProxy.java:38)
      at $Proxy45.getPrimaryKey(Unknown Source)
      at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.setInstanceValue(JDBCCMRFieldBridge.java:625)
      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.ejbproject.Customer.CustomerBean$Proxy.setHomeAddress()
      at com.ejbproject.Customer.CustomerBean.setAddress(CustomerBean.java:92)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:30)
      at sun.reflect.InflatableMethodAccessorImpl.invoke(InflatableMethodAccessorImpl.java:48)
      at java.lang.reflect.Method.invoke(Method.java:306)
      at org.jboss.ejb.EntityContainer$ContainerInterceptor.invoke(EntityContainer.java:1197)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor.invoke(JDBCRelationInterceptor.java:190)
      at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke(EntitySynchronizationInterceptor.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.invoke(EntityCreationInterceptor.java:69)
      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.EntityContainer.invoke(EntityContainer.java:493)
      at org.jboss.ejb.Container.invoke(Container.java:705)
      at org.jboss.ejb.EntityContainer.invoke(EntityContainer.java:1055)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:491)
      at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:362)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:30)
      at sun.reflect.InflatableMethodAccessorImpl.invoke(InflatableMethodAccessorImpl.java:48)
      at java.lang.reflect.Method.invoke(Method.java:306)
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:256)
      at sun.rmi.transport.Transport$1.run(Transport.java:156)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:152)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:469)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:706)
      at java.lang.Thread.run(Thread.java:579)

      Here is the ejb-jar.xml:

      <ejb-jar>
      <enterprise-beans>

      <ejb-name>CustomerEJB</ejb-name>
      com.ejbproject.Customer.CustomerHomeRemote
      com.ejbproject.Customer.CustomerRemote
      <ejb-class>com.ejbproject.Customer.CustomerBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>Customer</abstract-schema-name>
      <cmp-field><field-name>id</field-name></cmp-field>
      <cmp-field><field-name>lastName</field-name></cmp-field>
      <cmp-field><field-name>firstName</field-name></cmp-field>
      <cmp-field><field-name>hasGoodCredit</field-name></cmp-field>
      <primkey-field>id</primkey-field>
      <security-identity><use-caller-identity/></security-identity>


      <ejb-name>AddressEJB</ejb-name>
      <local-home>com.ejbproject.Customer.AddressHomeLocal</local-home>
      com.ejbproject.Customer.AddressLocal
      <ejb-class>com.ejbproject.Customer.AddressBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>Address</abstract-schema-name>
      <cmp-field><field-name>id</field-name></cmp-field>
      <cmp-field><field-name>street</field-name></cmp-field>
      <cmp-field><field-name>city</field-name></cmp-field>
      <cmp-field><field-name>state</field-name></cmp-field>
      <cmp-field><field-name>zip</field-name></cmp-field>
      <primkey-field>id</primkey-field>
      <security-identity><use-caller-identity/></security-identity>

      </enterprise-beans>


      <ejb-relation>
      <ejb-relation-name>Customer-Address</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>Customer-has-an-Address</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>CustomerEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>homeAddress</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>Address-belongs-to-Customer</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>AddressEJB</ejb-name>
      </relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>


      Here is the jbosscmp-jdbc.xml:

      <jbosscmp-jdbc>

      java:/MySqlDS
      <datasource-mapping>mySQL</datasource-mapping>
      <create-table>false</create-table>
      <remove-table>false</remove-table>

      <enterprise-beans>

      <ejb-name>CustomerEJB</ejb-name>
      <table-name>Customer</table-name>
      <cmp-field>
      <field-name>id</field-name>
      <column-name>ID</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>lastName</field-name>
      <column-name>last_name</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>firstName</field-name>
      <column-name>first_name</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>hasGoodCredit</field-name>
      <column-name>has_good_credit</column-name>
      </cmp-field>


      <ejb-name>AddressEJB</ejb-name>
      <table-name>Address</table-name>
      <cmp-field>
      <field-name>id</field-name>
      <column-name>ID</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>street</field-name>
      <column-name>street</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>city</field-name>
      <column-name>city</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>state</field-name>
      <column-name>state</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>zip</field-name>
      <column-name>zip</column-name>
      </cmp-field>



      </enterprise-beans>


      <ejb-relation>
      <ejb-relation-name>Customer-Address</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      Customer-has-an-Address
      </ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>
      Address-belongs-to-Customer
      </ejb-relationship-role-name>
      <fk-constraint>false</fk-constraint>
      <key-fields>
      <key-field>
      <field-name>id</field-name>
      <column-name>address_id</column-name>
      </key-field>

      </key-fields>
      </ejb-relationship-role>
      </ejb-relation>

      </jbosscmp-jdbc>

      Thanks for any help with this problem. Please be as specific as possible with your suggestions. Thanks in advance.

      Devon Coleman

        • 1. Re: CMP Relationship Problem
          Devon Coleman Newbie

          Hello Again:

          In case you are unable to find the specific error point here it is:

          2002-09-05 19:10:43,920 ERROR [org.jboss.ejb.plugins.LogInterceptor] TransactionRolledbackException, causedBy:
          java.lang.IllegalStateException: removing bean lock and it has tx set!


          What does this mean and how do I resolve the error? Thanks again.

          Devon C.

          • 2. Re: CMP Relationship Problem
            Fabricio Newbie

            Devon: i don't understand why you use address_id in the relationship section of the jbosscmp-jdbc file. Previously, you had said, in the jbosscmp-jdbc file...


            <ejb-name>AddressEJB</ejb-name>
            <table-name>Address</table-name>
            <cmp-field>
            <field-name>id</field-name>
            <column-name>ID</column-name>
            </cmp-field>
            <cmp-field>
            <field-name>street</field-name>
            <column-name>street</column-name>
            </cmp-field>
            <cmp-field>
            <field-name>city</field-name>
            <column-name>city</column-name>
            </cmp-field>
            <cmp-field>
            <field-name>state</field-name>
            <column-name>state</column-name>
            </cmp-field>
            <cmp-field>
            <field-name>zip</field-name>
            <column-name>zip</column-name>
            </cmp-field>



            can you explain this a little bit for me? thanks in advance.

            • 3. Re: CMP Relationship Problem
              Devon Coleman Newbie

              Hello:

              The reason why I use "address_id" field in the relationship mapping area of jbosscmp-jdbc.xml is that I am trying to have the value in the "id" field of the Address table placed into the "address_id" field of the Customer table. The specifics of the application are:
              A customer has one address and all address information is stored in the address table. Thus when a CustomerEJB is created an AddressEJB is created as well with the "id" cmp value of the AddressEJB being placed into the CustomerEJB's "address_id" field (all being handled by CMP/CMR). I have read the quick start manual concerning this and it appears that in order to have the "id" field value of the Address table placed into the "address_id" of the Customer table, establishing a Customer has an Address,I have to setup the relationship as I have in the jbosscmp-jdbc.xml file. Since that setup is not working I need some help understanding were I went wrong. Thanks

              Devon C.

              • 4. Re: CMP Relationship Problem
                Devon Coleman Newbie

                Hello:

                The reason why I use "address_id" field in the relationship mapping area of jbosscmp-jdbc.xml is that I am trying to have the value in the "id" field of the Address table placed into the "address_id" field of the Customer table. The specifics of the application are:
                A customer has one address and all address information is stored in the address table. Thus when a CustomerEJB is created an AddressEJB is created as well with the "id" cmp value of the AddressEJB being placed into the CustomerEJB's "address_id" field (all being handled by CMP/CMR). I have read the quick start manual concerning this and it appears that in order to have the "id" field value of the Address table placed into the "address_id" of the Customer table, establishing a Customer has an Address,I have to setup the relationship as I have in the jbosscmp-jdbc.xml file. Since that setup is not working I need some help understanding were I went wrong. Thanks

                Devon C.

                • 5. Re: CMP Relationship Problem
                  Devon Coleman Newbie

                  Hello:

                  The reason why I use "address_id" field in the relationship mapping area of jbosscmp-jdbc.xml is that I am trying to have the value in the "id" field of the Address table placed into the "address_id" field of the Customer table. The specifics of the application are:
                  A customer has one address and all address information is stored in the address table. Thus when a CustomerEJB is created an AddressEJB is created as well with the "id" cmp value of the AddressEJB being placed into the CustomerEJB's "address_id" field (all being handled by CMP/CMR). I have read the quick start manual concerning this and it appears that in order to have the "id" field value of the Address table placed into the "address_id" of the Customer table, establishing a Customer has an Address,I have to setup the relationship as I have in the jbosscmp-jdbc.xml file. Since that setup is not working I need some help understanding were I went wrong. Thanks

                  Devon C.

                  • 6. Re: CMP Relationship Problem
                    Devon Coleman Newbie

                    Hello:

                    The reason why I use "address_id" field in the relationship mapping area of jbosscmp-jdbc.xml is that I am trying to have the value in the "id" field of the Address table placed into the "address_id" field of the Customer table. The specifics of the application are:
                    A customer has one address and all address information is stored in the address table. Thus when a CustomerEJB is created an AddressEJB is created as well with the "id" cmp value of the AddressEJB being placed into the CustomerEJB's "address_id" field (all being handled by CMP/CMR). I have read the quick start manual concerning this and it appears that in order to have the "id" field value of the Address table placed into the "address_id" of the Customer table, establishing a Customer has an Address,I have to setup the relationship as I have in the jbosscmp-jdbc.xml file. Since that setup is not working I need some help understanding were I went wrong. Thanks

                    Devon C.