冬眠时,PostgreSQL:列英寸×"类型为OID,但前pression的类型是字节 [英] Hibernate, Postgresql: Column "x" is of type oid but expression is of type byte

查看:353
本文介绍了冬眠时,PostgreSQL:列英寸×"类型为OID,但前pression的类型是字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于含大对象Hibernate映射(BLOB)一个奇怪的问题,不同的数据库之间切换时。

  @Lob
私人字节[] binaryData;

本场以上MySQL和Oracle中创建一个字节数组场,然而,在PostreSQL它创建类型OID的领域。

现在,当我试图访问这个领域它工作在其他数据库罚款,但在PostgreSQL的失败,出现以下错误

 列binaryData的类型为OID,但前pression是bytea类型。

于是,我就简单地去掉@Lob的注释,这将解决PostgreSQL的问题,但是在MySQL中没有这个注解,休眠创造型TINYBLOB的领域,这是小我们大多数的情况下, 。并且,因为我们希望在一个以上的环境中使用本项目是恼人的有两个不同的映射切换

是否有强制PostgreSQL的使用bytea的,而不是OID与@Lob注释字段的任何注解?或者是它在某种程度上可以省略@Lob,把别的东西,以迫使MySQL的,因为它会使用@Lob具有较大的数据类型来分配呢?

我甚至能想象有这样一个解决方案。

 如果(字段类型的OID)
  它存储为OID
否则,如果(字段是bytea类型)
  它存储为BYTEA
其他
  //不能存储

和相同的吸气剂,如果存在的方式做种本

编辑:

下面的声明是工作。它分配色谱柱的oid,但是,使用这种知道如何存储和检索从这样的字段数据冬眠

  @Lob
@Type(TYPE =org.hibernate.type.PrimitiveByteArrayBlobType)
私人字节[] binaryFile;


解决方案

这字段映射在 org.hibernate.dialect.PostgreSQLDialect 定义,可以通过继承这个改变和配置您的应用程序与Postgres的运行时使用修改后的方言。

在子类中的相关咒语可能是把

  registerColumnType(Types.BLOBbytea的);

在)调用超(

在你的构造

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

@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.

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.

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.

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

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

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

解决方案

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" );

in your constructor after a call to super().

这篇关于冬眠时,PostgreSQL:列英寸×"类型为OID,但前pression的类型是字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆