-->
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.  [ 1 post ] 
Author Message
 Post subject: How do I retrieve blob value using native sql query
PostPosted: Fri Jun 03, 2005 11:09 am 
Newbie

Joined: Fri Jun 03, 2005 9:41 am
Posts: 1
Hi,

I’m using Hibernate 3.0 and Oracle9 DB.
(org.hibernate.dialect.Oracle9Dialect)

I wrote a sample code to retrieve a blob value from Oracle and update that value in another employee profile.

I used CreateSQLQuery method and executed a native sql query in hibernate for retrieving a blob value (EMP photo – image) from Oracle.

I get a class cast exception when I retrieve the value from the CreateSQLQuery method.


Exception:
--------------

SampleWrite method
Hibernate: select e.EMP_PHOTO as empPhoto from EMPLOYEE_MASTER e where e.EMP_ID = '1'
java.lang.ClassCastException
at com.sampleapp.Main.sampleWrite(Main.java:57)
at com.sampleapp.Main.main(Main.java:34)
Exception in thread "main"

How do I convert a Hibernate.BLOB value to oracle.sql.blob? and
How should I recieve the blob value when I'm using addScalar?


mapping file:
---------------


[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>

<class
name="com.sampleapp.EmployeeMaster"
table="EMPLOYEE_MASTER">

<id
name="empId"
type="java.lang.Integer"
column="EMP_ID"
>
<generator class="sequence">
<param name="sequence">SEQ_EMP_ID</param>
</generator>
</id>

<property
name="firstName"
type="java.lang.String"
column="FIRST_NAME"
length="50"
/>

<property
name="lastName"
type="java.lang.String"
column="LAST_NAME"
length="50"
/>

<property
name="empPhoto"
type="blob"
column="EMP_PHOTO"
length="4000"
/>

</class>
</hibernate-mapping>
[/code]


Sample Code:
--------------------

[code]

static void sampleWrite()
throws MappingException, HibernateException, Exception {

Session session = getHibernateSession();
Transaction save = session.beginTransaction();
System.out.println("SampleWrite method");

EmployeeMaster empMaster = new EmployeeMaster();

empMaster.setFirstName("FirstName");
empMaster.setLastName("LastName");


BLOB blob = (BLOB) session.createSQLQuery("select e.EMP_PHOTO as empPhoto from EMPLOYEE_MASTER e where e.EMP_ID = '1'")
.addScalar("empPhoto", Hibernate.BLOB)
.uniqueResult();

empMaster.setEmpPhoto(blob);
session.save(empMaster);
save.commit();
session.close();
}
[/code]


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

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.