Hi,
I have problem getting data from postgresql database using BLOB.
I'm using hibernate, jdbc-driver, postgresql.
I succesfully insert my large object into database:
Code:
Message message = new Message();
File file = new File("jude.ico");
InputStream in = null;
byte[] tmp = new byte[512];
ByteArrayOutputStream out = null;
try {
in = new FileInputStream( file );
out = new ByteArrayOutputStream();
int bytesRead = in.read(tmp);
while(bytesRead != -1) {
out.write(tmp, 0, bytesRead);
bytesRead = in.read(tmp);
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
byte[] data = out.toByteArray();
ByteArrayInputStream byteInput = new ByteArrayInputStream(data);
message.setData( data );
messageDao.saveOrUpdate( message );
My messageDao.saveOrUpdate( message ) is like this:
Code:
session = HibernateUtil.getSession( userRole );
session.saveOrUpdate( message );
HibernateUtil.commit( session );
Mapping file:
Code:
<hibernate-mapping>
<class name="mypac.Message" table="message">
.......
<property name="dataBlob">
<column name="message" sql-type="bytea" length="10024"/>
</property>
</class>
It insert data to database. I can read it from phppgadmin, it is something like: \000\000\001......
But when I call in next line for:
Code:
List messages = messageDao.findAll();
whitch make something like this:
Code:
session = HibernateUtil.getSession( userRole );
messages = session.find( "FROM Message m" );
HibernateUtil.commit( session );
I get exception:
DEBUG - SQLException
org.postgresql.util.PSQLException Bad Integer \000\000\001......
I read a lot of Blob/Clob using Postgresql and hibernate but I donot find answer what is worng.
I also tryied to create BinaryBlobType:
public class BinaryBlobType implements UserType and use it like a type in mapping file, but i get: ERROR: cross-database references are not implemented
Please help me.