-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: BinaryBlobType
PostPosted: Tue May 25, 2004 3:39 pm 
Beginner
Beginner

Joined: Wed Feb 25, 2004 10:58 am
Posts: 43
When my byte[] is null I am getting a nullpointer exception ? Anyone else have this problem ?

import java.io.IOException;
import java.io.OutputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Blob;

import oracle.sql.BLOB;

import net.sf.hibernate.Hibernate;

public class BinaryBlobType implements net.sf.hibernate.UserType {
public int[] sqlTypes() {
return new int[] { Types.BLOB };
}

public Class returnedClass() {
return byte[].class;
}

public boolean equals(Object x, Object y) {
return (x == y)
|| (x != null
&& y != null
&& java.util.Arrays.equals((byte[]) x, (byte[]) y));
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws SQLException {
if (st instanceof org.apache.commons.dbcp.DelegatingPreparedStatement
&& ((org.apache.commons.dbcp.DelegatingPreparedStatement) st)
.getDelegate()
instanceof oracle.jdbc.OraclePreparedStatement) {
oracle.sql.BLOB blob =
oracle.sql.BLOB.createTemporary(
((org.apache.commons.dbcp.PoolableConnection) st
.getConnection())
.getDelegate(),
false,
oracle.sql.BLOB.DURATION_SESSION);
blob.open(BLOB.MODE_READWRITE);

OutputStream out = blob.getBinaryOutputStream();

try {
out.write((byte[]) value);
out.flush();
out.close();
} catch (IOException e) {
throw new SQLException("failed write to blob" + e.getMessage());
}

blob.close();

((oracle
.jdbc
.OraclePreparedStatement)
((org
.apache
.commons
.dbcp
.DelegatingPreparedStatement) st)
.getDelegate())
.setBLOB(
index,
blob);
} else if (st instanceof oracle.jdbc.OraclePreparedStatement) {
oracle.sql.BLOB blob =
oracle.sql.BLOB.createTemporary(
st.getConnection(),
false,
oracle.sql.BLOB.DURATION_SESSION);

blob.open(BLOB.MODE_READWRITE);

OutputStream out = blob.getBinaryOutputStream();

try {
out.write((byte[]) value); // FAILS NULLPOINTER HERE
out.flush();
out.close();
} catch (IOException e) {
throw new SQLException("failed write to blob" + e.getMessage());
}

blob.close();

((oracle.jdbc.OraclePreparedStatement) (st)).setBLOB(index, blob);
} else {
st.setBlob(index, Hibernate.createBlob((byte[]) value));
}
}

// and.. note the null check, oracle drivers return a null blob...
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws SQLException {
final Blob blob = rs.getBlob(names[0]);
return blob != null ? blob.getBytes(1, (int) blob.length()) : null;
}

public Object deepCopy(Object value) {
if (value == null)
return null;

byte[] bytes = (byte[]) value;
byte[] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);

return result;
}

public boolean isMutable() {
return true;
}

}



I used the code off the wiki area . The comment in the code indicates where it fails.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.