2

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!

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
snlm95
  • 75
  • 2
  • 8
  • Have a look at http://stackoverflow.com/questions/10671471/how-to-store-image-into-postgres-database-using-hibernate and especially http://stackoverflow.com/questions/3677380/proper-hibernate-annotation-for-byte – DrColossos Jul 16 '13 at 08:23
  • Thanks. Looked at those before and they didn't help. – snlm95 Jul 21 '13 at 13:57

1 Answers1

3

change bytea_output='escape' in postgresql.conf

or run this

ALTER DATABASE dbname SET bytea_output TO 'escape';

ianegirl
  • 46
  • 1