4

I want to store image into database using hibernate and Java. I am using postgres database

I tried bytea data type to store image and byte[] data type in hibernate pojo.

I used the following code,

CREATE TABLE photo
(
  "photo_name" bytea
)
WITH (OIDS=FALSE);
ALTER TABLE photo OWNER TO postgres;

Hibernate Pojo

public class PhotoEntity {

byte[] name;

public byte[] getName() {
    return name;
}

public void setName(byte[] name) {
    this.name = name;
}

}

but it gives error at time of mapping.
please give me any reference to do this.

Dnyani
  • 1,133
  • 3
  • 11
  • 15
  • Show us your real code, and tell us the exact and complete error message, which probably contains meaningful information and is not just a "you did something wrong" flag. – JB Nizet May 20 '12 at 07:16
  • hi sir, error occure at time of mapping because bytea is not map with byte[] or byte also.so confused which data type i used instead of byte[] or byte in hibernate pojo. – Dnyani May 20 '12 at 07:36
  • 1
    You must supply the exact mapping code you used, and the error(s) you get, for anyone to help you. – Craig Ringer May 20 '12 at 10:21
  • did you figure this out? – Arya Nov 01 '17 at 22:18

1 Answers1

5

If you are using Hibernate via JPA2, you may need the @Lob annotation, though I'm not sure if that's for oid or bytea fields. See:

proper hibernate annotation for byte[]

There's also a Hibernate dev blog post that's quite informative.

If you're using Hibernate via XML mappings or its own annotations dialect, please show your exact code and error message(s).

See also the answers here.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • I suspect when `@Lob` mapping to `OID` chances are Hibernate won't `lo_unlink` the LOB neither before nor after losing reference to it via `DELETE`, `UPDATE`, etc. – 1737973 Jul 13 '17 at 20:58