3 Replies Latest reply on May 11, 2006 4:41 PM by zihong

    retrieving byte array from sybase ASA

    zihong

      How do I retrieve byte[] from sybases ASA? (I have not tried write to it yet. May need to write to it too.)

      I am using Sybase ASA 9, jconnect 5.5, EJB3 preview 5 in jboss 4.0.2. When I leave the sybase type-mapping in standardjbosscmp-jdbc.xml alone (ie, the byte, short mapping are commented out), and in my entity bean, I have

      @Lob(fetch = FetchType.EAGER)
      public byte[] getMasterMac() {
      return masterMac;
      }

      as did in examples in the EJB3 preview 5, then, I got error:

      java.lang.UnsupportedOperationException: The method com.sybase.jdbc2.jdbc.SybResultSet.getBlob(String) is not supported and should not be called.


      Sure enough, I checked jconnect document, it clearly indicated the getBlob(), getClob() methods are not supported.


      My next choice is to manually change the byte type mapping. Here are the 3 type mappings that I tried:

      (1) Uncomment out the original comments:



      <java-type>java.lang.Byte</java-type>
      <jdbc-type>TINYINT</jdbc-type>
      <sql-type>TINYINT</sql-type>



      (2)

      <java-type>java.lang.Byte</java-type>
      <jdbc-type>VARBINARY</jdbc-type>
      <sql-type>VARBINARY</sql-type>


      <java-type>byte[]</java-type>
      <jdbc-type>VARBINARY</jdbc-type>
      <sql-type>VARBINARY</sql-type>


      (3)

      <java-type>java.lang.Byte</java-type>
      <jdbc-type>VARBINARY</jdbc-type>
      <sql-type>VARBINARY</sql-type>


      <java-type>byte[]</java-type>
      <jdbc-type>VARBINARY(255)</jdbc-type>
      <sql-type>VARBINARY(255)</sql-type>



      I also tried to use BINARY instead of VARBINARY, none of them work. I always got error:

      2005-06-11 00:31:40,359 DEBUG [Loader]: result set row: 0
      2005-06-11 00:31:40,359 DEBUG [LongType]: returning '1' as column: id
      2005-06-11 00:31:40,359 DEBUG [Loader]: result row: EntityKey[com.myapp.beans.models.Node#1]
      2005-06-11 00:31:40,359 DEBUG [Loader]: Initializing object from ResultSet: [com.myapp.beans.models.Node#1]
      2005-06-11 00:31:40,359 DEBUG [BasicEntityPersister]: Hydrating entity: [com.myapp.beans.models.Node#1]
      2005-06-11 00:31:40,359 DEBUG [IntegerType]: returning '1' as column: state1_
      2005-06-11 00:31:40,359 DEBUG [StringType]: returning 'South' as column: domainName1_
      2005-06-11 00:31:40,375 DEBUG [IntegerType]: returning '100' as column: helloTim9_1_
      2005-06-11 00:31:40,375 DEBUG [AbstractBatcher]: about to close ResultSet (open ResultSets: 1, globally: 1)
      2005-06-11 00:31:40,390 DEBUG [AbstractBatcher]: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
      2005-06-11 00:31:40,390 DEBUG [AbstractBatcher]: closing statement
      2005-06-11 00:31:40,421 DEBUG [JDBCExceptionReporter]: could not execute query [select node0_.id as id, node0_.state as state1_, node0_.domainName as domainName1_, node0_.helloTimerInterval as helloTim9_1_, node0_.masterMac as masterMac1_ from Node node0_]
      java.sql.SQLException: JZ006: Caught IOException: java.io.IOException: JZ0EM: End of data.
      at com.sybase.jdbc2.jdbc.ErrorMessage.raiseError(ErrorMessage.java:485)
      at com.sybase.jdbc2.tds.Tds.handleIOE(Tds.java:2966)
      at com.sybase.jdbc2.tds.Tds.nextResult(Tds.java:2029)
      at com.sybase.jdbc2.tds.TdsResultSet.nextResult(TdsResultSet.java:512)
      at com.sybase.jdbc2.tds.TdsResultSet.next(TdsResultSet.java:298)
      at com.sybase.jdbc2.tds.TdsResultSet.close(TdsResultSet.java:394)
      at com.sybase.jdbc2.jdbc.SybResultSet.markDead(SybResultSet.java:1509)
      at com.sybase.jdbc2.jdbc.SybResultSet.close(SybResultSet.java:1567)
      at com.sybase.jdbc2.jdbc.SybResultSet.close(SybResultSet.java:182)
      at org.jboss.resource.adapter.jdbc.WrappedResultSet.internalClose(WrappedResultSet.java:808)
      at org.jboss.resource.adapter.jdbc.WrappedResultSet.close(WrappedResultSet.java:133)
      at org.hibernate.jdbc.AbstractBatcher.closeQueryStatement(AbstractBatcher.java:137)
      at org.hibernate.loader.Loader.doQuery(Loader.java:408)
      at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:206)
      at org.hibernate.loader.Loader.doList(Loader.java:1515)
      at org.hibernate.loader.Loader.list(Loader.java:1498)
      at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
      at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:266)
      at org.hibernate.impl.SessionImpl.list(SessionImpl.java:791)
      at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
      at org.jboss.ejb3.entity.QueryImpl.getResultList(QueryImpl.java:58)



      From the trace you can see that the retrieving did not get masterMac1_, the byte array (defined as binary(6) for the column type) column


      How should I solve this problem? Thanks in advance.


        • 1. Re: retrieving byte array from sybase ASA
          lindner

          I have a similar problem with Ingres. I use byte[] without a @LOB annotation.

          public setMasterMac(byte[] masterMac) {
          this.masterMac = masterMac;
          }

          When I try to insert a value into the databese (the column is of type "long binary") I got error

          Caused by: java.lang.ClassCastException: [B
          at org.hibernate.type.ByteType.set(ByteType.java:38)
          at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
          at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:39)

          The error message "... Ecxeption: [B" is strange. What means "[B". Why can't I insert a byte[] value into database? A pure JDBC test case with PreparedStatement.setBytes() works fine.

          • 2. Re: retrieving byte array from sybase ASA
            mat.robinson

            Did you ever solve this problem as I have exactly the same issue?

            • 3. Re: retrieving byte array from sybase ASA
              zihong

              With this combination, I seem to solve the problem a while back:

              JBoss 4.0.3 RCx or JBoss 4.0.4 CR2;
              Default standardjbosscmp-jdbc.xml setting for Sybase;
              Table column type: binary(16);


              On the field annotation:

              @Embedded
              @AttributeOverrides ({@AttributeOverride(name="ip", column=@Column(name="ip"))})
              public IpAddress getIp()
              {
              return ip;
              }
              public void setIp(IpAddress addr)
              {
              ip = addr;
              }


              On the embeddable class:

              @Embeddable
              public class IpAddress implements Serializable {
              private InetAddress ip;

              public void setIp(byte[] b) throws UnknownHostException
              {
              ip = InetAddress.getByAddress(b);
              }

              public byte[] getIp()
              {
              return ip.getAddress();
              }
              }


              You can see that eventually I still use the byte[] mapping. Though in my specific case, I used another type. I don't think the embeddable type solves this problem. But showing it here as what I have. Hope this will help.