I am having a very strange problem with storing the data from a JavaBean object with Hibernate 3.2 to an Oracle 10g database. The Hibernate operation which saves the object executes cleanly, no exceptions thrown. The problem is that the object contains a BLOB field, and though there is data in the object properties, including the BLOB field, the underlying table is populated in all fields except the BLOB field -- it remains empty. I'm not quite sure what the problem is, because no error is being thrown. I have included the mapping file, the JavaBean, and the Oracle table definition below.
Mapping document: (below)
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<class name="com.brad.Document" table="NODE_DOCUMENT">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" type="string" column="NAME" />
<property name="mimeType" type="string" column="MIME_TYPE" />
<property name="operationType" type="string" column="OPERATION_TYPE" />
<property name="dataBlob" type="blob" column="DOC_DATA" />
</class>
</hibernate-mapping>
JavaBean Object:Code:
package com.brad.entity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import org.hibernate.Hibernate;
public class Document {
private long id;
private String name;
private String mimeType;
private String operationType;
private byte[] dataBlob = new byte[0];
public byte[] getData() {
return dataBlob;
}
public void setData(byte[] data) {
this.dataBlob = data;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public String getOperationType() {
return operationType;
}
public void setOperationType(String operationType) {
this.operationType = operationType;
}
public String getDataAsString() {
return new String(dataBlob);
}
public void setDataBlob(Blob blob) {
this.dataBlob = this.convertBlobToByteArray(blob);
}
public Blob getDataBlob() {
return Hibernate.createBlob(this.dataBlob);
}
private byte[] convertBlobToByteArray(Blob blob) {
InputStream in = null;
ByteArrayOutputStream out = null;
byte[] buf = new byte[1024];
try {
in = blob.getBinaryStream();
out = new ByteArrayOutputStream();
while (true) {
int bytesRead = in.read(buf);
if (bytesRead == -1)
break;
out.write(buf, 0, bytesRead);
}
return out.toByteArray();
} catch (SQLException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
}
}
}
Oracle table definition:Code:
CREATE TABLE "NODE_DOCUMENT"
( "ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(250),
"MIME_TYPE" VARCHAR2(250),
"OPERATION_TYPE" VARCHAR2(20) NOT NULL ENABLE,
"DOC_DATA" BLOB
) ;
ALTER TABLE "NODE_DOCUMENT" ADD CONSTRAINT "NODE_DOCUMENT_PK" PRIMARY KEY ("ID") ENABLE;
Any ideas? Your help is appreciated!
Thanks,
Brad