Hibernate postgres bytea retrieval issue

2019-04-14 00:26发布

问题:

I am using Hibernate 4.0 to store jpegs into postgres 9.1.4 (jdbc is postgresql-9.1-901.jdbc4.jar) bytea column(byte[] is the hibernate entity, without extra type def).

The hibernate storing process works fine because I can use Database tools to dump the bytea column and still get the jpegs. It is basically:

In the managedBean

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

At this point, the bytes look like [-1, -40, -1, -32, 0, 16, 74, 70, ...]

However, the problem starts with I am retrieving via hibernate. The data seems modified or corrupted somehow.

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

At this point, the bytes become [-26, 100, 56, 102, 102, 101, 48, 48,...]

The hibernate getter is

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

Appreciate if anyone can assist, thanks!

回答1:

change bytea_output='escape' in postgresql.conf

or run this

ALTER DATABASE dbname SET bytea_output TO 'escape';