2009-09-07 116 views
7

我有一個將PDF報告保存到Oracle數據庫。 報告的dataType是一個byteArray。Groovy域映射

的域定義如下:

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

byte[] report 
Company company 
Date month 

}

可惜,這定義了在Oracle數據庫具有RAW DATA_TYPE和255

應如何lenghth場 我將該字段定義爲域類? 應該定義爲BLOB?

如果是,如何做到這一點?

在此先感謝。

回答

7

255是提供給byte []的默認大小。按照您的要求,在約束中指定報告的最大大小。例如:

static constraints = { 
    report(maxSize: 50000000) 
} 

根據最大尺寸,DB中的字段類型將被設置。 (MEDIUMBLOB,LONGBLOB等)

1

類型儘量明確設置到任何一個「blob」或「二進制」,例如,你可以添加以下域類:

static mapping = { 
    report type:'blob' 
} 
1

這裏有一個blog article,有望解決這個問題。訣竅似乎是有一個java.sql.Blob類型的字段,byte[]字段從該字段派生並標記爲瞬態。

1

基於邁克爾博格瓦特答案,這裏是我做過什麼來解決這個問題:

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 
    } 

}