-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: BLOB data not saving in Oracle 10g
PostPosted: Fri Mar 16, 2007 12:10 am 
Newbie

Joined: Thu Aug 10, 2006 1:50 am
Posts: 8
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


Top
 Profile  
 
 Post subject: Re: BLOB data not saving in Oracle 10g
PostPosted: Fri Mar 16, 2007 3:39 am 
Newbie

Joined: Wed Dec 06, 2006 2:40 am
Posts: 2
Location: HYDERABAD
Can you send the entire code to my mail-id so that i can help you

vijay.maddala@gmail.com

bye


brado77 wrote:
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

_________________
M.VIJAY


Top
 Profile  
 
 Post subject: Re: BLOB data not saving in Oracle 10g
PostPosted: Fri Mar 16, 2007 10:04 am 
Newbie

Joined: Thu Aug 10, 2006 1:50 am
Posts: 8
Quote:
Can you send the entire code to my mail-id so that i can help you


Vijay,

Thanks for the reply. I'd be happy to send whatever you need -- but what is attached here in this email is nearly everything. Except for the save call on the session, this is the entire code. Was there something addtiional that you needed?

Let me know and I'll send it.

Thanks!

Brad


Top
 Profile  
 
 Post subject: problem solved...
PostPosted: Fri Mar 16, 2007 10:50 pm 
Newbie

Joined: Thu Aug 10, 2006 1:50 am
Posts: 8
The answer was to change the access attribute to "property" on the blob property in the mapping file.

Brad


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.