2

我正在使用 Hibernate 4.0 将 jpeg 存储到 postgres 9.1.4 中(jdbc 是 postgresql-9.1-901.jdbc4.jar) bytea 列(byte[] 是休眠实体,没有额外的类型 def)。

休眠存储过程运行良好,因为我可以使用数据库工具转储 bytea 列并仍然获得 jpeg。基本上是:

在 managedBean

byte [] bytes;
bytes = IOUtils.toByteArray(file.getInputstream());
entity.setImage(bytes);

此时,字节看起来像 [-1, -40, -1, -32, 0, 16, 74, 70, ...]

但是,问题始于我通过休眠进行检索。数据似乎以某种方式被修改或损坏。

byte [] bytes;
bytes = entity.getImage();

此时,字节变为 [-26, 100, 56, 102, 102, 101, 48, 48,...]

休眠吸气剂是

@Column(name = "image")
public byte[] getImage() {
    return image;
}

如果有人可以提供帮助,不胜感激,谢谢!

4

1 回答 1

3

在 postgresql.conf 中更改 bytea_output='escape'

或运行这个

ALTER DATABASE dbname SET bytea_output TO 'escape';

于 2013-07-29T09:44:48.963 回答