Tuesday, November 15, 2011

type-mappinmg for byte[] in standardjbosscmp-jdbc.xml

http://community.jboss.org/thread/23104?decorator=print&displayFullThread=true

 

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

 

Cheers,

Sarang

No comments:

Post a Comment