New to using the syntax of Named SQL. Using Hibernate 3.2. Is the return type java.lang.Object, or the mapped object itself, when I use select SQL?
At the present time, the return from Select statement is returning java.lang.Object. What setup do I need to get the custom mapped object (org.poc.MyObject), instead of java.lang.Object?
This is how I am calling the named query:
Code:
Query query = session.getNamedQuery("getMyResult");
List codeList = query.list();
for(int i = 0; i<codeList.size(); i++){
MyObject myObj = (MyObject)codeList.get(i);
}
codeList contains a list of java.lang.Object, that are queried from database. The list of returned java.lang.Object has all the queried data types, however, the returned object is java.lang.Object. Instead, I want to get a list of org.poc.MyObject. Casting the returned java.lang.Object to org.poc.MyObject inside the for loop throws exception as:
Exception[Ljava.lang.Object; incompatible with org.poc.MyObject
Following is MyObject.hbm.xml
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 package="org.poc" >
<class name="MyObject" table="MY_TABLE" lazy="false">
<id name="id" column="ID" type="int">
</id>
<property name="desc" column="DESC" type="string" />
<property name="name" column="NAME" type="string" />
</class>
<sql-query name="getMyResult"><![CDATA[
select ID, DESC, NAME from MY_TABLE
]]>
</sql-query>
</hibernate-mapping>