2009-09-07 86 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 
    } 

}