Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring, Blob download, byte[] size increases twice

I am using Spring 3.0 with Hibernate and PostgreSQL and I have following problem:
I'm uploading the files to the database, saving its content type, and everything works ok, the size of the field in database is OK. However when I try to download it, Hibernate returns the byte array twice as bigger as it should. The downloaded files are corrupted of course. The size is ESXACTLY 2 times bigger then the size in database... My code looks as follow:
The field domain class (with mapping):

private byte[] cv;

@Column(name="cv")
public byte[] getCv() {
  return this.cv;
}

The DAO function that loads the object:

public Candidate load(Integer id) {
  return (Candidate) getHibernateTemplate().get(Candidate.class, id);
}

Session factory config:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.jdbc.batch_size">30</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

    <property name="packagesToScan" value="foo.foo.core.domain"/>

</bean>

The SQL Create:

CREATE TABLE candidate
(
  id serial NOT NULL,
  cv bytea,
  ...
)

The database is enncoded UTF-8 if it make any difference.
I was trying with org.springframework.jdbc.support.lob.DefaultLobHandler, but it does not do the thing.. Any ideas? It's driving me crazy..

like image 911
Mat Avatar asked Oct 11 '22 16:10

Mat


1 Answers

found the solution, change your DB setting for bytea output as follows:

ALTER DATABASE SET bytea_output='escape';

Oscar.

like image 150
Oscar Ruiz Avatar answered Oct 18 '22 10:10

Oscar Ruiz