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:
- Is there reliable way to store and read binary data using JPA2?
- Is there way to read binary data in postgres? Because right now I am not sure if they are even correctly stored.
BYTEA
if the data is less than a few megabytes. It typically maps cleaner into an ORM. You might want to read this thread