[ http://jira.andromda.org/browse/HIB-166?page=comments#action_12415 ]
     
Darius Schier commented on HIB-166:
-----------------------------------

I reopened this jira, as the implementation within 3.1 caused some trouble:


AndroMDA 3.1, Spring, Hibernate.
Setting a NULL value into a blob lead to an Exception with PostgreSQL (and 
maybe others?):

14:15:13,399 ERROR JDBCExceptionReporter:72 - insert into "CORE_BLOB" 
(modification, "NAME", "IDENT", "QUARANTINE_USER", "QUARANTINE_DATE", 
"CREATE_DATE", "CREATE_USER", "MODIFY_DATE", "MODIFY_USER", "SOME_BLOB", "ID") 
values (0, theBlob, theBlob, NULL, NULL, 2006-01-17 
14:15:13,399  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 42804
14:15:13,399 ERROR JDBCExceptionReporter:72 - ERROR: column "SOME_BLOB" is of 
type bytea but expression is of type oid

The following lines fix the problem and a proper NULL is inserted into the 
column at the database.

    public void nullSafeSet(PreparedStatement st, Object value, int index)
        throws SQLException {
        byte[] b = (byte[]) value;
        if (b != null) {
           st.setBinaryStream(index, new ByteArrayInputStream(b), b.length);
        } else {
           try {
              st.setBinaryStream(index, null, 0);
           } catch (SQLException e) {
              st.setBlob(index, null);
           }
        }
    }

-------------------
Very simular with nullSafeGet: The current implementation leads to Errors 
(again PostgreSQL), even if there is no NULL in the database.

Hibernate: select this_."ID" as ID1_11_0_, this_.modification as 
modifica2_11_0_, this_."NAME" as NAME3_11_0_, this_."IDENT" as IDENT4_11_0_, 
this_."QUARANTINE_USER" as QUARANTINE5_11_0_, this_."QUARANTINE_DATE" as 
QUARANTINE6_11_0_, this_."CREATE_DATE" as CREATE7_11_0_, this_."CREATE_USER" as 
CREATE8_11_0_, this_."MODIFY_DATE" as MODIFY9_11_0_, this_."MODIFY_USER" as 
MODIFY10_11_0_, this_."SOME_BLOB" as SOME11_11_0_ from "CORE_BLOB" this_
14:23:44,230  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 22003
14:23:44,245 ERROR JDBCExceptionReporter:72 - Unzulässiger Wert für den Typ int 
: \000\001\002\003.

where the some_blob is of type bytea (as expected).

The following code seems to do the job:

    public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
        throws SQLException
    {
       InputStream is = resultSet.getBinaryStream(names[0]);
       if( is == null) {
          return null;
       }
       ByteArrayOutputStream os = new ByteArrayOutputStream(70000);
       byte[] buffer = new byte[65536];
       try {
          while (is.read(buffer) > -1) {
             os.write(buffer);
          }
       } catch (IOException e) {
          Log.error("Unable to read blob " + names[0], e);
       }
    }




> CLONE -nullSafeGet not null safe HIB-103
> ----------------------------------------
>
>          Key: HIB-166
>          URL: http://jira.andromda.org/browse/HIB-166
>      Project: Hibernate Cartridge
>         Type: Bug
>     Versions: 3.0 Final
>  Environment: HIbernate/Spring cartridge
>     Reporter: Darius Schier
>     Assignee: Wouter Zoons

>
> Hi there,
> HibernateByteBlobType.nullSafeGet and nullSafeSet are not null save, NPE are 
> thrown in both cases if the value is null.
> I would suggest something like that:
>     public void nullSafeSet(PreparedStatement st, Object value, int index)
>         throws SQLException
>     {
>         byte[] b = (byte[])value;
>         if(b!= null ) {
>            st.setBinaryStream(index, new ByteArrayInputStream(b), b.length);
>         } else {
>            st.setBinaryStream(index,null,0);
>         }
>     }
>     public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
>         throws SQLException
>     {
>         Blob blob = rs.getBlob(names[0]);
>         if(blob == null) {
>             return null;
>         } else {
>             return blob.getBytes(1, (int)blob.length());
>         }
>     }
> Regards
> Darius




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid3432&bid#0486&dat1642

Reply via email to