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