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.

4
  • 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. Commented May 20, 2012 at 7: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. Commented May 20, 2012 at 7:36
  • 1
    You must supply the exact mapping code you used, and the error(s) you get, for anyone to help you. Commented May 20, 2012 at 10:21
  • did you figure this out? Commented Nov 1, 2017 at 22:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.