Wednesday, May 21, 2008

Mapping a document from byte[] to BLOB in Grails on Oracle

For CDR ( I had to upload a document of any sort into an oracle database. Using a byte[] type worked just fine with the embedded HSQL database but when I deployed it to Oracle 9i or 11g (that's our platforms) it failed horribly. This was apparently due to hibernate creating in Oracle a 'RAW' object with a maximum size of 255. I'm not an Oracle DBA so I don't know if that is appropriate but it wasn't working.

My second thought was to change the object type in my domain object to blob:

static mapping = {
document type: 'blob'

But that didn't work either because it couldn't automatically convert a byte[] to a BLOB. I was able to find others who had had similar issues on the Grails nabble and a blog posting by a gentleman named Ryan using hibernate to convert byte[] to BLOB were old (by Grails standards) and I wanted to see if Grails could handle this using the new ORM DSL instead of hibernate mapping files. This is what I came up with:

import java.sql.Blob
import java.sql.SQLException
import org.hibernate.Hibernate

class Documentation extends ConfigurationItem {
int docVersion
byte[] document
Blob documentBlob
String fileType
String fileName
String fileSize
String title
String abstraction
DocumentationType documentationType

Date dateCreated
Date lastUpdated

static transients = ["document"]
static belongsTo = DocumentationType
static constraints = {
docVersion(nullable: true)
document(nullable: true)
documentBlob(nullable: true)
fileType(nullable: true)
fileName(nullable: true)
fileSize(nullable: true)
title(nullable: true)
abstraction(nullable: true)
documentationType(nullable: true)

static mapping = {
documentBlob type: 'blob'

def getDocument() {
if (documentBlob == null)
return null;
return toByteArray(getDocumentBlob());

def setDocument(document) {

boolean equals(obj) {
if (this == obj) return true;
if (!obj || obj.class != this.class) return false;
return id?.equals(

int hashCode() {
return id ? id.hashCode() : super.hashCode()

private byte[] toByteArray(Blob fromBlob) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return toByteArrayImpl(fromBlob, baos);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (baos != null) {
try {
} catch (IOException ex) {

private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos) {
byte[] buf = new byte[4000];
InputStream is = fromBlob.getBinaryStream();
try {
while (true) {
int dataSize =;

if (dataSize == -1)
baos.write(buf, 0, dataSize);
} finally {
if (is != null) {
try {
} catch (IOException ex) {
throw ex;
return baos.toByteArray();

String toString() {
return "${name}"

Notice all I had to do was create an ORM mapping for documentBlob. So far so good! It seems to be working in our testing in both HSQL and Oracle 9i. Has anyone else ran into this issue?

Thursday, May 8, 2008

Notes from the Groovy and Grails meetup

I went to the Groovy and Grails meetup which was basically the highlight of CommunityOne, although it wasn't technically part of communityOne. It was put on by No Fluff Just Stuff and G2One.

It started with Guilluime giving the state of the Union on Groovy and Grails. Then came folks from LinkedIn talking about their experiences using Grails and finally by Mathew Porter of Contegix talking about why they use Grails internally instead of Rails as they do a tremendous
amount of Rails hosting. This was very interesting and he made two big points.
  1. Grails scales for less cost (hardware)
  2. The Grails platform is more stable and they don't have to constantly upgrade the technologies, re-train developers, etc.

After the presentations they formed a panel of No Fluff Just Stuff experts and talked about subjects like the future of Java, where languages fit with the platform, SOA, etc. It was a lively discussion and interesting to see the experts contemplating the same issues that the rest of us humble developers have.

It was nice to meet and talk to folks there. I met Dave Klein JUG leader (from where evades me), Jeff Brown and Guillaume Laforge of G2One fame, Dierk Konig , writer of the fantastic Groovy In Action (GINA) and my beloved Webtest plugin for Grails, and my favorite Tech writer Scott Davis who is also the Chief Editor of (correction, I had before stated that Scott Davis was the CE of which is Glen Smith). All very cool people to talk too and very passionate about groovy and grails.

Tuesday, May 6, 2008

JavaOne: Day 1 (Tuesday): Keynote

Knowledge Dump:
  • Java is on 3 Billion Devices(mostly phones)
  • Card scanners, blah blah blah, old news
  • John Gage just made a joke (sorta) about not having a social security card with an electronic identifier, or as he said:
    "Hi, I'm an American, please detonate the closest device."
  • CO2 sensor, used for mining, etc. Can be used for monitoring water usage, power usage, unsafe gas levels such as CO2 etc.]
  • Gas meater can monitor 40 different times of gas
  • Amazon Kindle (little electronic book) done in Java
  • Very cool mash-up Demo of a multi-media app done in JavaFX on applet, desktop, and mobile
  • one thing was cool is that they were able to drag the applet to the desktop and drag photos from the desktop to the application, it felt very integrated
  • They were having ridiculous network issues, then again so am I
  • Very cool 3D high-def stuff, very fast, very cool looking stuff
  • Java 6 update 10 preview release
  • JavaFX Desktop SDK EAP (July)
  • JavaFX Desktop 1.0 (Fall)
  • JavaFX Mobile and TV (Spring 2009)
  • Glassfish V3 Kernal
    • 98KB
    • Modular Design
    • Okay I'm a glassfish fanboy
  • 48,000,000 JRE downloads a month
  • Project Hydrazine
    • Find
    • Merge
    • Deploy
    • Share
    • Monetize
  • Project Insight
    • JavaFX platform will have "Instrumentation", the ability to monitor data of users and passing data to users.
    • I don't really understand what that means.
  • Holy Crap, Neil Young is up on stage
    • He's talking about how the Blue Ray sound is so much superior to anything prior
    • He's wearing shades, what the...
    • That was very cool suff, best Blue Ray Demo Ever

Monkey Search