2012-02-02 84 views
4

紧跟在this example上,我正在上传一个小文件并尝试存储到postgresql bytea列中。缝文件上传到postgres bytea列“列是bytea但表达式是bigint”

下面是错误(第一两个输出记录语句的INSERT之前输出bean的属性尝试:

SAGE 1 -- action.registration.LetterTemplateHome - content type: text/xml

SAGE 1 -- action.registration.LetterTemplateHome - letterTemplateText: [[email protected]

SAGE 1 -- action.registration.LetterTemplateHome - contents as String: xml version="1.0" encoding="UTF-8" standalone="yes" .... etc

SAGE 1 -- org.hibernate.util.JDBCExceptionReporter - Batch entry 0 insert into letter_template (content_type, file_name_template, fileSize, letter_template_name, letter_template_text, letter_template_id) values ('text/xml', 'letterDate.xml', '0', 'yu', '37078', '202') was aborted. Call getNextException to see the cause.

SAGE 1 -- org.hibernate.util.JDBCExceptionReporter - ERROR: column "letter_template_text" is of type bytea but expression is of type bigint Hint: You will need to rewrite or cast the expression. Position: 162

这里是如何该字段在bean定义:

private byte[] letterTemplateText; 

@Lob 
@Column(name = "letter_template_text") 
@Basic(fetch = FetchType.LAZY) 
public byte[] getLetterTemplateText() { 
    return this.letterTemplateText; 
} 

public void setLetterTemplateText(byte[] letterTemplateText) { 
    this.letterTemplateText = letterTemplateText; 
} 

回答

0

这并没有真正回答你的问题,但我想我会分享...

有两种方式来存储文件的数据库的帮助,真的:存储a文件的实际内容(就像你在做的一样),只存储文件路径(并将其保存在实际的文件系统中)。

我已经使用过这两种方法,我更喜欢后者,原因有两个:我可以将文件移动到其他硬盘驱动器,分区甚至通过共享访问它们,我需要做的就是更改文件路径在数据库中。此外,它使数据库转储(又名备份)更小,更快地执行。

+1

感谢您的评论。只要将上传的文件存储在文件系统中,我们的环境就会被锁定。我必须有一个非常好的理由来解决方案。另外,我们有负载均衡器,它们很好地处理数据库更改。我不知道他们将如何处理文件系统文件。有问题的数据量非常小,不会影响性能。而且,我认为在数据库和文件系统之间建立这种联系确实会非常脆弱。 – mcgyver5 2012-02-02 16:42:40

2

我怀疑Hibernate试图在PostgreSQL中使用“大对象”方法,它涉及在表中的文件中存储一个OID“句柄”。一些示例如下:http://virgo47.wordpress.com/2008/06/13/jpa-postgresql-and-bytea-vs-oid-type/

如果您想坚持使用bytea列(并且在SQL方面使用起来相当简单),请使用BinaryType来映射列。请参阅:proper hibernate annotation for byte[]

+0

谢谢。你的第一个链接明确描述了问题。但是,当我去到不同版本的postgresql(和另一个驱动程序)时,问题就消失了。我将发布哪些驱动程序/哪些postgresql版本/哪些注释协同工作的图表。 – mcgyver5 2012-02-03 15:14:29

相关问题