13 Replies Latest reply on Oct 11, 2002 10:23 AM by Dmitry Mozheyko

    ClassCastException thrown by ejbCreate()

    Dave Martin Newbie

      I am using JBoss3.0.2 with DB2. I have a simple EntityBean with 2 CMP fields alert_id and batch_id both of type integer.

      public Integer ejbCreate (Integer batch_id, Integer alert_id) throws CreateException {

      System.out.println ("[TestTableBean] Starting create method...");
      System.out.println("[TestTableBean] Values ="+batch_id.toString() + alert_id.toString());
      setAlert_id (alert_id);
      setBatch_id (batch_id);
      System.out.println ("[TestTableBean] Finishing create method...");

      return null;
      }

      When this method is executed the following exception is thrown.

      2002-10-10 14:13:43,843 INFO [STDOUT] [TestTableBean] Starting create method...
      2002-10-10 14:13:43,859 INFO [STDOUT] [TestTableBean] Values =44
      2002-10-10 14:13:43,859 INFO [STDOUT] [TestTableBean] Finishing create method...
      2002-10-10 14:13:43,859 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.TestTable] Create: pk=4
      2002-10-10 14:13:43,859 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.TestTable] Executing SQL: SELECT COUNT(*) FROM ALR.TESTTABLE WHERE alert_id=?
      2002-10-10 14:13:43,859 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.TestTable] Executing SQL: INSERT INTO ALR.TESTTABLE (alert_id, batch_id) VALUES (?, ?)
      2002-10-10 14:13:43,859 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.TestTable] Rows affected = 1
      2002-10-10 14:13:43,875 ERROR [org.jboss.ejb.plugins.LogInterceptor] RuntimeException:
      java.lang.ClassCastException: $Proxy507
      at $Proxy501.create(Unknown Source)
      at com.searchspace.research.onlineservices.ejb.alert.session.AlertDelegateBean.createAlert(AlertDelegateBean.java:91)
      at java.lang.reflect.Method.invoke(Native Method)
      at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:660)
      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:107)
      at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:178)
      at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:60)
      at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:130)
      at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:203)
      at org.jboss.ejb.StatelessSessionContainer.invoke(StatelessSessionContainer.java:313)
      at org.jboss.ejb.Container.invoke(Container.java:720)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:517)
      at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:370)
      at java.lang.reflect.Method.invoke(Native Method)
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:236)
      at sun.rmi.transport.Transport$1.run(Transport.java:147)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:143)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
      at java.lang.Thread.run(Thread.java:479)

      I am using the correct DB2 type mappings as far as I know (I am using the db2-service.xml template supplied in the release).

      Any help would be greatly appreciated!

        • 1. Re: ClassCastException thrown by ejbCreate()
          Dmitry Mozheyko Novice

          And the data are inserted into the table?

          • 2. Re: ClassCastException thrown by ejbCreate()
            Alexey Loubyansky Master

            The stacktrace says that exception is thrown after the bean is created in session container. Post the code from session bean that creates the entity.
            Note, entity's home create method returns interface type (local or remote depending on the home's type) and not Integer as ejbCreate.

            • 3. Re: ClassCastException thrown by ejbCreate()
              Dave Martin Newbie

              The code runs OK when the return type is the local interface. I thought EJB2.0 Entity beans should be designed so that the return type is the primary key type for that entity bean. This is the case in many examples in books (and the CMP Gangster example with the JBoss Quick Start doc). Anyhow, thanks for you help with this.

              • 4. Re: ClassCastException thrown by ejbCreate()
                Dave Martin Newbie

                The data is not inserted.

                • 5. Re: ClassCastException thrown by ejbCreate()
                  Dmitry Mozheyko Novice

                  Please send fragments ejb-jar, jbosscmp-jdbc, Home, Remote and Bean sources

                  • 6. Re: ClassCastException thrown by ejbCreate()
                    Dave Martin Newbie

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


                    <![CDATA[No Description.]]>
                    <display-name>TestTable</display-name>

                    <ejb-name>TestTable</ejb-name>

                    <local-home>com.searchspace.research.onlineservices.ejb.alert.interfaces.TestTableLocalHome</local-home>
                    com.searchspace.research.onlineservices.ejb.alert.interfaces.TestTableLocal

                    <ejb-class>com.searchspace.research.onlineservices.ejb.alert.entity.TestTableBean</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>ALR</abstract-schema-name>
                    <cmp-field >
                    <![CDATA[]]>
                    <field-name>alert_id</field-name>
                    </cmp-field>
                    <cmp-field >
                    <![CDATA[]]>
                    <field-name>batch_id</field-name>
                    </cmp-field>
                    <primkey-field>alert_id</primkey-field>



                    jbosscmp-jdbc
                    ---------------
                    <jbosscmp-jdbc>

                    java:/DB2DS
                    <datasource-mapping>DB2</datasource-mapping>


                    <enterprise-beans>

                    <ejb-name>TestTable</ejb-name>
                    <create-table>false</create-table>
                    <table-name>ALR.TestTable</table-name>

                    <cmp-field>
                    <field-name>alert_id</field-name>
                    <column-name>alert_id</column-name>
                    <jdbc-type>INTEGER</jdbc-type>
                    <sql-type>INTEGER</sql-type>

                    </cmp-field>
                    <cmp-field>
                    <field-name>batch_id</field-name>
                    <column-name>batch_id</column-name>
                    <jdbc-type>INTEGER</jdbc-type>
                    <sql-type>INTEGER</sql-type>

                    </cmp-field>
                    <cmp-field>
                    <field-name>alert_id</field-name>
                    <column-name>alert_id</column-name>
                    <jdbc-type>INTEGER</jdbc-type>
                    <sql-type>INTEGER</sql-type>

                    </cmp-field>
                    <cmp-field>
                    <field-name>batch_id</field-name>
                    <column-name>batch_id</column-name>
                    <jdbc-type>INTEGER</jdbc-type>
                    <sql-type>INTEGER</sql-type>

                    </cmp-field>
                    <!--
                    To define load groups for this entity, write a file named
                    jbosscmp-jdbc-load-TestTableBean.xml and put it in
                    your mergedir. Write it according to the following DTD slice:
                    <!ELEMENT load-groups (load-group+)>
                    <!ELEMENT load-group (load-group-name, description?, field-name+)>
                    <!ELEMENT load-group-name (#PCDATA)>
                    <!ELEMENT description (#PCDATA)>
                    <!ELEMENT field-name (#PCDATA)>
                    <!ELEMENT eager-load-group (#PCDATA)>
                    <!ELEMENT lazy-load-groups (load-group-name+)>
                    -->



                    LocalHome Interface

                    public interface TestTableLocalHome
                    extends javax.ejb.EJBLocalHome
                    {
                    public static final String COMP_NAME="java:comp/env/ejb/TestTable";
                    public static final String JNDI_NAME="ejb/TestTable";

                    public TestTableLocal findByPrimaryKey(java.lang.Integer pk) throws javax.ejb.FinderException;

                    public java.lang.Integer create(java.lang.Integer batch_id,java.lang.Integer alert_id) throws javax.ejb.CreateException;

                    }

                    Local Interface
                    ---------------

                    public interface TestTableLocal
                    extends javax.ejb.EJBLocalObject
                    {

                    public java.lang.Integer getAlert_id( ) ;
                    public java.lang.Integer getBatch_id( ) ;
                    public void setAlert_id( java.lang.Integer alert_id ) ;
                    public void setBatch_id( java.lang.Integer batch_id ) ;

                    }

                    • 7. Re: ClassCastException thrown by ejbCreate()
                      Dmitry Mozheyko Novice

                      And Bean code ..................

                      • 8. Re: ClassCastException thrown by ejbCreate()
                        Dmitry Mozheyko Novice

                        public java.lang.Integer create(java.lang.Integer batch_id,java.lang.Integer alert_id) throws javax.ejb.CreateException;

                        ERROR not java.lang.Integer

                        NEED TestTableLocal

                        • 9. Re: ClassCastException thrown by ejbCreate()
                          Dave Martin Newbie

                          In many examples in EJB books (and the CMP Gangster example with the JBoss Quick Start doc) the return type for ejbCreate method is the primary key type for that entity bean. I realise now it must be the interface type. Thanks for you help.

                          • 10. Re: ClassCastException thrown by ejbCreate()
                            Dmitry Mozheyko Novice

                            NO ejbCreate
                            IN THE LocalHome interface !!!!!!!!!!!!!!!!!!!

                            • 11. Re: ClassCastException thrown by ejbCreate()
                              Dmitry Mozheyko Novice

                              You are mistaken, in an example the Gangster all is normal

                              Bean abstact class

                              GangsterBean
                              .................
                              Integer ejbCreate(...)
                              ...................

                              LocalHome interface

                              GangsterHome
                              ................................
                              Gangster create(...)
                              ..............................

                              • 12. Re: ClassCastException thrown by ejbCreate()
                                Dave Martin Newbie

                                Yes you are right. I stand corrected.

                                I think my problem is that I am using XDoclet to create the LocalHome and Local interfaces. When I do this with my bean's ejbCreate method like so:

                                /**
                                * @ejb:home-method view-type="local"
                                */
                                public Integer ejbCreate (Integer batch_id, Integer alert_id)

                                the method is represented in the LocalHome interface like so:

                                public java.lang.Integer create(java.lang.Integer batch_id,java.lang.Integer alert_id) throws javax.ejb.CreateException;

                                Where as it should be :

                                public TestTableLocal create(java.lang.Integer batch_id,java.lang.Integer alert_id) throws javax.ejb.CreateException;

                                Anyway, the problem is resolved. Thanks for your help.

                                • 13. Re: ClassCastException thrown by ejbCreate()
                                  Dmitry Mozheyko Novice

                                  I do not use XDoclet all I write handles