7 Replies Latest reply on May 19, 2004 5:37 PM by john_anderson_ii

    java.lang.ClassCastException

    nacho2004

      Hi,
      I've 2 beans one-one relationships
      and this piece of code

      InitialContext ctx = new InitialContext();
      SDocuments myBean = getHome().create();
      Collection col = myBean.getAllDocumentByCategoryId(11);
      Iterator iter = col.iterator();
      while (iter.hasNext()) {
      EDocuments element = ((EDocuments) iter.next());
      System.out.println("Abstract:" + element.getShortabstract());
      EDocMetaData metaData = element.getDocMetaData(); System.out.println("Rawtext:" + metaData.getRawtext());
      }
      } catch (NamingException e) {
      e.printStackTrace();
      }

      in the bold line this error appear:
      java.lang.ClassCastException at com.Entrieva.Skyline.EntityBeans.EDocumentsCMP$Proxy.getDocMetaData()


      the ejb-jar relationships is :
      <!-- Relationships -->

      <ejb-relation >
      <ejb-relation-name>EDocuments-EDocMetaData</ejb-relation-name>

      <ejb-relationship-role >
      <ejb-relationship-role-name>EDocuments-has-one-EDocMetadata</ejb-relationship-role-name>
      One
      <relationship-role-source >
      <ejb-name>EDocuments</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>docMetaData</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>

      <ejb-relationship-role >
      <ejb-relationship-role-name>EDocMetadata-has-one-EDocuments</ejb-relationship-role-name>
      One
      <relationship-role-source >
      <ejb-name>EDocMetaData</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>documents</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>

      </ejb-relation>

        • 1. Re: java.lang.ClassCastException
          sesques

          Hi,

          Post your code and jbosscmp-jdbc.xml.
          Also the full error log.

          Pascal

          • 2. Re: java.lang.ClassCastException
            nacho2004


            <ejb-relation>
            <ejb-relation-name>EDocuments-EDocMetaData</ejb-relation-name>
            <foreign-key-mapping/>
            <ejb-relationship-role>
            <ejb-relationship-role-name>EDocuments-has-one-EDocMetadata</ejb-relationship-role-name>
            <key-fields/>
            </ejb-relationship-role>
            <ejb-relationship-role>
            <ejb-relationship-role-name>EDocMetadata-has-one-EDocuments</ejb-relationship-role-name>
            <key-fields>
            <key-field>
            <field-name>docid</field-name>
            <column-name>docid</column-name>
            </key-field>
            </key-fields>

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

            • 3. Re: java.lang.ClassCastException
              sesques

              Hi,

              Your relation defined as a one-one looks like a one-many.
              The ClassCastException can occur if the result is a collection, not a single row.
              Try to define your relation as a one-many, with a collection as return type of the getter.

              Pascal

              • 4. Re: java.lang.ClassCastException
                nacho2004

                do you have any document to implement a one-to-one relationships
                I think the @ejb.relation is good in both case (2 beans) but I can't understand why I can't call one bean to other by getDocidMetaData() method.
                thanks in advance

                • 5. Re: java.lang.ClassCastException
                  sesques

                  Your relation is a good one.
                  I tell you the problem can occurs if the result of the query (managed by the container for the relationship) is a multi-row instead of a single row.
                  Do you have potentially many EDocMetaData for one EDocuments ?
                  In this case, your relation MUST BE a one-many.
                  With the informations you give, this is the only problem I think about.
                  I ask you to post your code and the whole descriptors, not only a snippet.
                  But you cannot set a one-one relationship if it is a many-one in reality.

                  Seems more clear ?

                  Pascal

                  • 6. Re: java.lang.ClassCastException
                    nacho2004

                    But in my case both tables contain the same amount of row
                    And the data model for those tables is one-to-one relationships

                    • 7. Re: java.lang.ClassCastException
                      john_anderson_ii

                      I don't know very much about CMR, I'm learning that now. But I think the ClassCastException could possibly come from these lines:

                      Collection col = myBean.getAllDocumentByCategoryId(11);
                      -----SNIP ------------

                      EDocuments element = ((EDocuments) iter.next());


                      Unless you can guaruntee that every item in the collection returned by getAllDocumentsByCategoryId is of type Edocuments, or can be cast to type Edocuments.