Hibernate,Postgresql:列“x"是 oid 类型,但表达式是 byte 类型

编程入门 行业动态 更新时间:2024-10-28 08:22:00
本文介绍了Hibernate,Postgresql:列“x"是 oid 类型,但表达式是 byte 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在不同数据库之间切换时,我遇到了一个关于包含大对象 (BLOB) 的休眠映射的奇怪问题.

I have a strange problem regarding the hibernate mapping containing large objects (BLOB), when switching between different databases.

@Lob private byte[] binaryData;

上面的字段在 MySQL 和 Oracle 中创建了一个字节数组字段,但是在 PostreSQL 中它创建了一个 oid 类型的字段.

The field above creates a byte array field in MySQL and in Oracle, however in PostreSQL it creates a field of type oid.

现在,当我尝试访问此字段时,它在其他数据库中工作正常,但在 PostgreSQL 中失败并出现以下错误

Now when I try to access this field it works fine in the other databases, but in PostgreSQL it fails with the following error

Column "binaryData" is of type oid but expression is of type bytea.

所以我尝试简单地删除@Lob"注释,这将解决 PostgreSQL 的问题,但是在没有这个注释的 MySQL 中,hibernate 创建了一个类型为tinyblob"的字段,在我们的大多数情况下它都很小.而且,由于我们想在多个环境中使用这个项目,所以要切换两种不同的映射是很烦人的.

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.

是否有任何注释强制 postgreSQL 对使用 @Lob 注释的字段使用 bytea 而不是 oid?或者是否有可能省略@Lob并放置其他内容以强制MySQL为其分配更大的数据类型,就像使用@Lob一样?

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?

我什至可以想象有这样的解决方案

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

如果有办法做到这一点,则与 getter 相同

and the same as a getter, if there exists a way to do kind of this

以下声明有效.它将列分配为 oid,但是使用它的休眠知道如何从此类字段中存储和检索数据

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

@Lob @Type(type="org.hibernate.type.PrimitiveByteArrayBlobType") private byte[] binaryFile;

推荐答案

这个字段映射在 org.hibernate.dialect.PostgreSQLDialect 中定义,可以通过子类化和配置你的应用来改变它使用 postgres 运行时修改的方言.

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.

子类中的相关咒语大概就是放

The relevant incantation in the subclass is probably to put

registerColumnType( Types.BLOB, "bytea" );

在你的构造函数中调用 super().

in your constructor after a call to super().

更多推荐

Hibernate,Postgresql:列“x"是 oid 类型,但表达式是 byte 类型

本文发布于:2023-10-16 10:06:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   表达式   Postgresql   Hibernate   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!