7

I have a to save a pdf report into an Oracle DB. The report's dataType is a byteArray.

The domain definition is as follows:

static constraints = {
 report(nullable:false)
 company(nullable:false)    
 month(nullable:false)    
}

byte[] report
Company company
Date month

}

Unfortunately this defines in the Oracle DB a field which has A RAW data_type and a lenghth of 255.

How should I define this field into the domain class? Should be defined as a BLOB?

If yes, How to do this?

Thanks in advance.

4

4 回答 4

7

255 is the default size provided to a byte[]. Specify the max size for report in constraints as per your requirement. Something like:

static constraints = {
    report(maxSize: 50000000)
}

Based on max size, the field type in DB will be set. (mediumblob, longblob etc.)

于 2009-09-07T17:02:50.093 回答
1

Try explicitly setting the type to either a 'blob' or 'binary', for example you can add the following to the domain class:

static mapping = {
    report type:'blob'
}
于 2009-09-07T15:54:25.800 回答
1

Here's a blog article that promises to solve this problem. The trick seems to be to have a field of type java.sql.Blob, with the byte[] field derived from that and marked as transient.

于 2009-09-07T15:55:54.617 回答
1

Based on Michael Borgwardt answer, here is what I did to solve this problem:

import java.sql.Blob

import org.hibernate.lob.BlobImpl

class Pagina {

    Blob reportBlob

    static mapping = {
        reportBlob column: 'PAGI_TX_DADOS', type: 'blob'
    }

    def setReport(byte[] bytes) {
        if (bytes != null) {
            ByteArrayInputStream bais =  new ByteArrayInputStream(bytes)
            int length =  bytes.length
            reportBlob = new BlobImpl(bais,length)      
        }
    }

    def getReport() {
        return reportBlob?.binaryStream
    }

}
于 2012-04-20T18:56:21.167 回答