0

I am trying to store an image in the database but when I read the value I am getting always null.

I've tried several combinations of following annotations but result is always same:

@Lob
@Basic(fetch = FetchType.EAGER)
@Type(type="org.hibernate.type.BinaryType")
private byte[] image;

persistence.xml:

<persistence-unit name="primary" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/WheelGoDS</jta-data-source>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.current_session_context_class" value="jta"/>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.connection.useUnicode" value="true" />
        <property name="hibernate.connection.characterEncoding" value="UTF-8" />
    </properties>
</persistence-unit>

When I use only @Lob I've noticed that using pgAdmin i see numeric value. Otherwise I see binary data. My questions are:

  1. Is there reliable way to store and read binary data using JPA2?
  2. Is there way to read binary data in postgres? Because right now I am not sure if they are even correctly stored.
2
  • What is the type of the column in PostgreSQL? I would recommend BYTEA if the data is less than a few megabytes. It typically maps cleaner into an ORM. You might want to read this thread
    – brettw
    Commented Dec 21, 2013 at 11:50
  • When I use anotations mentioned in my primary post the type is BYTEA. Do you know how to view/save its content?
    – Martin Nuc
    Commented Dec 21, 2013 at 17:00

0

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.