-->
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.  [ 2 posts ] 
Author Message
 Post subject: Named SQL in Hibernate 3.2-select returns java.lang.Object
PostPosted: Fri Aug 08, 2008 5:04 pm 
Newbie

Joined: Mon Feb 26, 2007 5:23 pm
Posts: 5
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>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 08, 2008 5:22 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There are plenty of examples of this in the Hibernate documentation:
http://www.hibernate.org/hib_docs/v3/re ... medqueries

Basically, you have to specify a <return class="MyObject"> tag inside the <sql-query> tag.


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

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.