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
:-(
Instead of setting the type to blob try to increase the maxSize constraint:
static constraints = {
rawFile(maxSize: 20 * 1024 * 1024) // 20 MBs
// ...
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With