I have found a issue (bug?) that hibernate would truncate blob (by oid) in PostgreSQL after updating the entity, even the code just reading blob binary stream:
Hibernate version: 3.2.1
Mapping documents:
Code:
public class SubjectFile {
private Long id;
private Blob data;
private String filename;
//omit getter and setter
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="antar.acquisition.file" >
<class name="SubjectFile" table="subject_file" lazy="true" >
<id name="id" column="id">
<generator class="sequence">
<param name="sequence">seq_subject_file</param>
</generator>
</id>
<property name="data" not-null="true" /> <!-- blob-->
<property name="filename" length="255" not-null="true" unique="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
//Read and update in transaction
Transaction transaction = session.beginTransaction();
SubjectFile loadFile = (SubjectFile) session.get(SubjectFile.class, new Long(12));
byte[] readBytes = IOUtils.toByteArray(loadFile.getData().getBinaryStream());
assertEquals(1000, readBytes.length); // data is 1000 bytes.
loadFile.setFilename("makeSomeChange.txt");
// fileName got updated but blob is truncated to 0 byte at database!
session.saveOrUpdate(reload);
transaction.commit();
session.close();
Name and version of the database you are using:
PostgreSQL 8.1 with jdbc driver 8.2-504
The generated SQL (show_sql=true):
Hibernate: update subject_file set data=?, filename=? where id=?
Any help on this issue ? should I file a issue on JIRA ?