Hibernate,Postgresql:列“x”的类型为 oid 但表达式的类型为字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4488693/
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
Hibernate, Postgresql: Column "x" is of type oid but expression is of type byte
提问by Hons
I have a strange problem regarding the hibernate mapping containing large objects (BLOB), when switching between different databases.
在不同数据库之间切换时,我有一个关于包含大对象 (BLOB) 的休眠映射的奇怪问题。
@Lob
private byte[] binaryData;
The field above creates a byte array field in MySQL and in Oracle, however in PostreSQL it creates a field of type oid.
上面的字段在 MySQL 和 Oracle 中创建了一个字节数组字段,但是在 PostreSQL 中它创建了一个 oid 类型的字段。
Now when I try to access this field it works fine in the other databases, but in PostgreSQL it fails with the following error
现在,当我尝试访问此字段时,它在其他数据库中运行良好,但在 PostgreSQL 中失败并显示以下错误
Column "binaryData" is of type oid but expression is of type bytea.
So I tried to simply remove the "@Lob" annotation, which will solve the problem for PostgreSQL, however in MySQL without this annotation, hibernate creates a field of type "tinyblob", which is to small in most of our cases. And, as we want to use this project in more than one environment it is annoying to have two different mappings to switch.
所以我试图简单地删除“@Lob”注释,这将解决 PostgreSQL 的问题,但是在没有这个注释的 MySQL 中,hibernate 会创建一个“tinyblob”类型的字段,在我们的大多数情况下它很小。而且,由于我们想在多个环境中使用这个项目,所以切换两个不同的映射很烦人。
Is there any annotation that forces postgreSQL to use bytea instead of oid for fields annotated with @Lob? Or is it somehow possible to omit the @Lob and put something else in order to force MySQL to allocate it with a larger datatype as it would using @Lob?
是否有任何注释强制 postgreSQL 对用@Lob 注释的字段使用 bytea 而不是 oid?或者是否有可能以某种方式省略@Lob 并添加其他内容以强制 MySQL 使用更大的数据类型分配它,就像使用 @Lob 一样?
I could even imagine to have a solution like this
我什至可以想象有这样的解决方案
if (field is of type oid)
store it as oid
else if (field is of type bytea)
store it as bytea
else
// not storable
and the same as a getter, if there exists a way to do kind of this
和吸气剂一样,如果有办法做到这一点
EDIT:
编辑:
The following declaration is working. It allocates the column as oid, however hibernate using this knows how to store and retrieve data from such a field
以下声明有效。它将列分配为 oid,但是使用它的休眠知道如何从这样的字段中存储和检索数据
@Lob
@Type(type="org.hibernate.type.PrimitiveByteArrayBlobType")
private byte[] binaryFile;
采纳答案by Don Roby
This field mapping is defined in org.hibernate.dialect.PostgreSQLDialect
and can be changed by subclassing this and configuring your app to use the modified dialect when running with postgres.
此字段映射定义于org.hibernate.dialect.PostgreSQLDialect
并且可以通过将其子类化并将您的应用程序配置为在使用 postgres 运行时使用修改后的方言来更改。
The relevant incantation in the subclass is probably to put
子类中的相关咒语大概是把
registerColumnType( Types.BLOB, "bytea" );
in your constructor after a call to super()
.
在您的构造函数中调用super()
.
回答by rogerdpack
For me this may have meant once "revert your postgres jdbc version back down to 9.3-1101.jdbc4"
对我来说,这可能意味着曾经“将您的 postgres jdbc 版本还原到 9.3-1101.jdbc4”
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc41</version>
</dependency>
worked as well. Newer than that failed...
也有效。比那个更新失败了...