PostgreSQL bytea列的Hibernate映射

时间:2018-10-22 07:52:20

标签: postgresql hibernate-mapping

我正在从DB2迁移到PostgreSQL。结果,我在PostgreSQL中提出了以下DDL:

CREATE TABLE binResTable (
        idField       DECIMAL(15) NOT NULL,
        checksumField DECIMAL(15) DEFAULT 0 NOT NULL,
        blobField     bytea NOT NULL DEFAULT E'\\000'
    );

我的Hiberante映射(XML文件)看起来如下:

BinaryResource.hbm.xml

<hibernate-mapping>
<class name="BinaryResource" table="binResTable" lazy="true">
    <id name="id" type="long" column="idField" />
    <property name="checksum" type="long" column="checksumField" lazy="true" />
    <property name="bytes" type="BinaryBlobType" column="blobField" lazy="true" /> 
</class>
</hibernate-mapping>

以下是映射的Java类:

public class BinaryResource implements Serializable {

    private long id;

    private BinaryDocument document;

    private byte[] bytes;

    private long checksum;

    ...

对于Blob字段,我使用以下UserType:

import java.io.Serializable;
import java.sql.Blob;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class BinaryBlobType implements UserType {

    public int[] sqlTypes() {
        return new int[] { Types.BLOB };
    }

    public Class<byte[]> returnedClass() {
        return byte[].class;
    }

    ...

读取数据工作正常,但是当我想在表“ binResTable”中插入条目时,出现以下错误:

ERROR: column "blobField" is of type bytea but expression is of type bigint

如何在Hibernate中映射bytea字段?

0 个答案:

没有答案