2 Replies Latest reply on May 28, 2004 7:51 PM by null null

    CMP & blob's

    car Newbie

      Hi

      My question is how do I read and write to a CMP entity bean's blob object ?

      I have an CMP entity bean which maps to a table in a MS SQL server database containing a blob field of type 'image' which is translates to a jdbc LONGVARBINARY sql type.

      The standardjbosscmp-jdbc.xml maps the MS SQL image type to a java object as follows:


      <java-type>java.lang.Object</java-type>
      <jdbc-type>JAVA_OBJECT</jdbc-type>
      <sql-type>IMAGE</sql-type>


      My entity bean has methods as follows:

      public abstract java.lang.Object getNte_memo();
      public abstract void setNte_memo(java.lang.Object value);

      This all works fine.

      But what do I do with this object returned in order to read the blob's data and how do I write data back to the blob via a java.lang.Object ?

      thanks in advance,
      CaR

        • 1. Re: CMP & blob's
          car Newbie

          After many hours investigating this problem I have found a solution myself.

          Here the mapping required in standardjbosscmp-jdbc.xml if you want to read and write to blob fields using an byte[] java type rather than some serialized java.lang.Object:


          <java-type>[B</java-type>
          <jdbc-type>VARBINARY</jdbc-type>
          <sql-type>IMAGE</sql-type>


          and the entity bean abstract get & set methods will be as below:

          public abstract byte[] getField();
          public abstract void setField(byte[] value);

          Now I can read and write to the blob using array's of bytes just like any byte[] which is simple, neat and tidy coding exercise.

          I guess you would need to be a bit careful with really big blobs as when the entity bean is read from the DB it will bring the whole blob into memory and hold it in a byte[].

          The <java-type> of '[B' means a byte[] type. This you will not find in any documentation. I found this by looking at the JBoss source code in the process of trying to understand what was going on with blobs. The class in where I found the '[B' is:

          org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil

          CaR.

          • 2. Re: CMP & blob's
            null null Newbie

            You can always rely on the container to figure out the java type from your abstract cmp field methods.

            The only thing you need to do in jbosscmp-jdbc is to define specific jdbc/sql mapping (without saying anything about the java-type. So "[B" really is optional.

            Here is an example in jbosscmp-jdbc.

            <cmp-field>
            .........
            <jdbc-type>VARCHAR</jdbc-type>
            <sql-type>BLOB</sql-type>

            </cmp-field>