This HELP:
Because some Database not support BLOB (Access,..) .
In case java call Blob.getBytes and throw UnsupportedOperationException : Blob may not be manipulated from creating session
You must test DB support or not support BLOB ?
if support you can using Hibernate.createBlob in you code to create BLOB
if not you using: new javax.sql.rowset.serial.SerialBlob(bytes) to create BLOB
See my code here:
Code:
package com.perppos.hibernate.utils;
public class HibernateUtil {
private static boolean blobable = false;
private static boolean testtedBlobable = false;
// You code here .....
private static void doTestBlobable() {
System.out.println(" CALL TEST BLOBABLE ... ");
try {
Session s = sessionFactory.openSession();
s.beginTransaction();
byte[] testBytes = "Test".getBytes();
Blob b = Hibernate.createBlob(testBytes);
TestTable temp = new TestTable();
temp.setId(UUID.randomUUID().toString());
temp.setBlobContent(b);
s.persist(temp); // ==> If DB Type not support BLOB(MS Access,..) ==> throw UnsupportedOperationException
s.delete(temp);
s.getTransaction().commit();
s.close();
blobable = true;
System.out.println(" ==> BLOBABLE");
} catch (UnsupportedOperationException e) {
// Blob may not be manipulated from creating session
blobable = false;
System.out.println(" ==> NOT BLOBABLE");
}
testtedBlobable = true;
}
/**
* Using Hibernate.createBlob : Cause raise Error for Access DB
* @param bytes
* @return
*/
public static Blob createBlob(byte[] bytes) {
if (!testtedBlobable) {
doTestBlobable();
}
if (blobable) {
return Hibernate.createBlob(bytes);
} else {
return new javax.sql.rowset.serial.SerialBlob(bytes);
}
}
}
.
Code:
public class TestTable {
private String id;
private Blob blobContent;
public TestTable() {
}
public Blob getBlobContent() {
return blobContent;
}
public void setBlobContent(Blob blobContent) {
this.blobContent = blobContent;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
}
TestTable.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 25, 2010 2:44:05 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.perppos.hibernate.entities.TestTable" table="TEST_TABLE">
<id name="id" type="string">
<column name="ID"/>
<generator class="assigned"/>
</id>
<property name="blobContent" type="blob">
<column name="BLOB_CONTENT"/>
</property>
</class>
</hibernate-mapping>