Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I create a file upload in grails which works with oracle?

got the following problem:

I tried to create a simpel file upload functionality in grails. I just created a domain class with a

byte[] rawFile

as property. Grails did most of the rest for me. It worked fine for the standard hsqldb in the dev environment.

Then I deployed it to the server with an oracle db configured (thin driver). Everything but the file upload works fine with the oracle db. For the file upload I get a (as far as I can remember)

SQLException: ORA-01461: can bind a LONG value only for insert into a LONG

I tried several ways to fix it (including some columnmappings to blobs and using java.sql.blob instead of byte[]) but nothing really worked and I went in a direction where my code wouldn't be db independent anymore.

Google didn't really help me and my grails books don't help too.

Saving the file to the disk isn't a good solution im my opinion.

So here's my question:

how do I create a file upload in grails which works with oracle?

Update: got some additional infos. Managed to reproduce the problem with the XE-Edition of Oracle:

Hibernate creates a VARBINARY(255) column for the rawFile. So I tried to upload a 4 bytes file and it worked.

I then changed the type of the column manually to 'blob' and it worked with bigger files.

I then added

static mapping = {
    columns {
        rawFile type:'blob'
    }
}

to my domain class and it stopped working:

ERROR errors.GrailsExceptionResolver - [B cannot be cast to java.sql.Blob java.lang.ClassCastException: [B cannot be cast to java.sql.Blob

:-(

like image 961
rdmueller Avatar asked Jan 08 '11 19:01

rdmueller


2 Answers

Instead of setting the type to blob try to increase the maxSize constraint:

static constraints = {
    rawFile(maxSize: 20 * 1024 * 1024) // 20 MBs
    // ...
}
like image 150
Abdullah Jibaly Avatar answered Oct 12 '22 22:10

Abdullah Jibaly


If you want to use a Blob field in Oracle then set your domain property to byte[] and set the type to org.hibernate.type.MaterializedBlobType. The MaterializedBlobType handles conversion back and forth between Oracle (presumably other databases, but I've only done this on Oracle) and byte[].

byte[] blobFile

static mapping = {
    blobFile type: org.hibernate.type.MaterializedBlobType
}
like image 30
Ed OConnor-Giles Avatar answered Oct 12 '22 22:10

Ed OConnor-Giles