Thread:
 ClassCastException using Hibernate2 Blobs 
 dannwebster   25 Mar 2004, 01:15 
 Re: ClassCastException using Hibernate2 Blobs 
 dannwebster   26 Mar 2004, 19:23 
 Re: ClassCastException using Hibernate2 Blob... 
 leenamba   30 Mar 2004, 11:21 
 Re: ClassCastException using Hibernate2 B... 
 alu1344   08 Jun 2004, 09:20 
 Re: ClassCastException using Hibernate2 Blobs 
 zetacode   21 Nov 2006, 11:09 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: dannwebster (25 Mar 2004, 01:15) Replies: 4, Views: 44355
Subject: ClassCastException using Hibernate2 Blobs
The following could be regarded as a trivial question, but it seems to
be a common problem, since variations of it show up in the forums
several times. So that the forums have a central reference for this
problem, I think that this page should explain why this problem occurs
and how to overcome it. The Problem:

I am using the following code to access blobs (instead of clobs. It is
the trivial modification of the code included on this page, but
presumably this problem exists on both blob and clob types...if not, the
distinction should be noted on this page):

<code>
s = sf.openSession();
tx = s.beginTransaction();
foo = new Foo();
foo.setBlob( Hibernate.createBlob(new byte[1]) );
s.save(foo);
s.flush();
s.refresh(foo, LockMode.UPGRADE); //grabs an Oracle BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) foo.getBlob();
OutputStream os = blob.getBinaryOutputStream();
os.write(content);
os.flush();
os.close();
tx.commit();
s.close();
</code>

This code always throws a ClassCastException on this line:

<code>
oracle.sql.BLOB blob = (oracle.sql.BLOB) foo.getBlob();
</code>

When hibernate gets the Blob, it is of type
net.sf.hibernate.lob.BlobImpl, which is not castable to oracle.sql.BLOB.
Since BlobImpl is what Hibernate uses to implement any type which is
generated using a "blob" type in the .hbm.xml file, and it makes sense
that it is not a subclass of oracle.sql.BLOB. 

The alternative would seem to be to define your "blob" in the .hbm.xml
file as "oracle.sql.BLOB," but then this line will fail to compile:

<code>
foo.setBlob( Hibernate.createBlob(new byte[1]) );
</code>

because Hibernate.createBlob() creates a java.sql.Blob, which is not
castable to foo's blob property, since it is now set to a specific
implementation (oracle.sql.BLOB) instead of the generalized
java.sql.Blob interface.

Is there some setting that needs to be used in the .hbm.xml file to make
the Hibernate BlobImpl castable to oracle.sql.BLOB, or is there
something specific to Blobs as opposed to Clobs that makes this occur?
Or am I fundamentally misunderstanding something else that I should get
from reading this page?
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]