postgresql 接缝文件上传到 postgres bytea 列“列是 bytea 但表达式是 bigint 类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9114510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:35:36  来源:igfitidea点击:

seam file upload to postgres bytea column " column is bytea but expression is of type bigint"

postgresqlseam

提问by mcgyver5

Closely following this example, I'm uploading a small file and trying to store into postgresql bytea column.

密切关注此示例,我正在上传一个小文件并尝试存储到 postgresql bytea 列中。

Here is error (first two outputs are logging statements outputting attributes of bean before the INSERT is attempted:

这是错误(前两个输出是在尝试 INSERT 之前输出 bean 属性的日志记录语句:

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

SAGE 1 -- action.registration.LetterTemplateHome - letterTemplateText: [B@48c7aaef

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

SAGE 1 -- action.registration.LetterTemplateHome - 内容类型:text/xml

SAGE 1 -- action.registration.LetterTemplateHome - letterTemplateText: [B@48c7aaef

SAGE 1——action.registration.LetterTemplateHome——内容为字符串:xml version="1.0" encoding="UTF-8" standalone="yes" .... etc

SAGE 1 -- org.hibernate.util.JDBCExceptionReporter - 批量条目 0 插入到 letter_template (content_type, file_name_template, fileSize, letter_template_name, letter_template_text, letter_template_id) 值 ('text/xml', 'letterDate.xml', '0', ' yu', '37078', '202') 被中止。调用 getNextException 以查看原因。

SAGE 1 -- org.hibernate.util.JDBCExceptionReporter - 错误:“letter_template_text”列的类型为 bytea,但表达式的类型为 bigint 提示:您需要重写或转换表达式。职位:162

here is how the field is defined in the bean:

这是在 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;
}

回答by araqnid

I suspect that Hibernate is trying to use the "large object" method with PostgreSQL, which involves storing an OID "handle" to the file in the table. Some example reading: http://virgo47.wordpress.com/2008/06/13/jpa-postgresql-and-bytea-vs-oid-type/

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

If you want to stick with just using a bytea column (and this is considerably simpler to work with on the SQL side), use BinaryType to map the column. See: proper hibernate annotation for byte[]

如果您想坚持只使用 bytea 列(这在 SQL 端使用起来要简单得多),请使用 BinaryType 来映射该列。请参阅:byte [] 的正确休眠注释

回答by Ilya Yevlampiev

The similar error may appear if you fave <loadData/>with missed columntags inside and some csv file, with, say, path to blob files in some csv column used for populating blobs column in the table. It doesn't point to "wow, you don't have columns there in loadData tag", but assume all the csv columns are string data and then throws the said error (but writes column is bytea but expression is of type string). You just need to specify all columns of the table explicitly with columntags and provide their data type.

如果您喜欢在内部<loadData/>缺少column标签和一些 csv 文件,例如在用于填充表中 blob 列的某个 csv 列中的 blob 文件的路径,则可能会出现类似的错误。它并不指向“哇,你在 loadData 标签中没有列”,而是假设所有 csv 列都是字符串数据,然后抛出所述错误(但写入column is bytea but expression is of type string)。您只需要使用column标签显式指定表的所有列并提供它们的数据类型。

回答by Alessandro

For me it's working with type byteain Postgres database.

对我来说,它正在处理byteaPostgres 数据库中的类型。

@Lob
@Type(type="org.hibernate.type.BinaryType")
@Column(name = "photo")
private byte[] photo;

//and get and set

回答by Andre

This doesn't really answer your question, but I thought I'd share anyway...

这并不能真正回答你的问题,但我想我还是会分享......

There are two ways of storing files with the help of a database, really: storing the actual contents of the file (like you're doing) and storing only the file path (and saving it in the actual filesystem).

在数据库的帮助下有两种存储文件的方法,实际上:存储文件的实际内容(就像您正在做的那样)和仅存储文件路径(并将其保存在实际文件系统中)。

I've worked with both methods and I preferred the latter for two reasons: I can move my files around to other hard drives, partitions and even access them through a share and all I need to do is change the file paths in the database. Also, it makes database dumps (aka backups) a lot smaller and faster to perform.

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