0

when I try to convert Oject into blob using (Blob), getting that java.lang.ClassCastException: [B cannot be cast to java.sql.Blob error.

but when I try to convert using the following code and write as an image file, gets the image corrupted.

Blob blob = null;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = null;
        try {
          out = new ObjectOutputStream(bos);   
          out.writeObject(myObj);

          byte[] yourBytes = bos.toByteArray();

          //blob.setBytes(1, yourBytes );
          blob = new SerialBlob(yourBytes);
        } finally {
          out.close();
          bos.close();
        }

How can I safely convert object (which is an image file saved as Blob) to Blob?

NOTE: due to requirement, I can only get as Object passing from other part.

5
  • Your question is very unclear. If the object you are trying to save is already a blob then why are you trying to convert it again? Are you sure your object isn't a buffered image? Commented Apr 4, 2013 at 3:42
  • first, the image is saved as blob in DB. when get from DB, the other method return me as an object , not as a blob. Commented Apr 4, 2013 at 3:45
  • To find out what class of the object being returned just do a System.out.println(obj.getClass()). Add the result of the printout to your question. Commented Apr 4, 2013 at 3:50
  • when I print out, get class [B Commented Apr 4, 2013 at 3:58
  • So your method is not returning a Blob, its returning an array of bytes. Which you can actually convert directly into a Blob, via its constructor. Commented Apr 4, 2013 at 3:59

3 Answers 3

1

You can use

If you are using hibernate

      Blob blob = Hibernate.createBlob(bytes, session); 

If you are using JDBC

       Blob blob = connection.createBlob();
       blob.setBytes(1, bytes);
Sign up to request clarification or add additional context in comments.

Comments

0

Because of the class cast exception you are receiving I'm going to go out on a limb here and say you are not actually getting a Blob from that end.

Do a debug and see what type is actually being sent.

And I may be wrong but as far as I know Clobs and Blobs are not serializable... So if the other side is a network, it will be kaboom...

Comments

0

Not sure how relevant my answer is but have used the above answers as hint to arrive at this.

Using - below dependency

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.1.0.Final</version>
</dependency>

Property in model class was defined as -

java.sql.Blob message;

In mymodel.hbm.xml file inside a valid model mapping

<class name="packagepath.Pojo" table="pojotableindb">
<meta attribute="class-description">
     Pojo class is mapped to pojotableindb table
  </meta>
    <id name="id" column="ID" type="long">
    <!-- for auto increment -->
        <generator class="identity" />
    </id>
    <property name="message" column="MESSAGE" type="blob" />
</class>

Now, inside one of my DAO method I did something as below to insert a value in MESSAGE column -

Pojo pojo = new Pojo();
pojo.setMessageId(uuid);
//message is the string argument this DAO method receives         
activityTrack.setMessage(Hibernate.getLobCreator(session).createBlob(message.getBytes())); 

tx = session.beginTransaction();
session.save(pojo);
tx.commit(); 

Hoping this will help someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.