-->
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.  [ 3 posts ] 
Author Message
 Post subject: Results not being mapped to the correct class
PostPosted: Fri Jun 13, 2008 12:41 pm 
Newbie

Joined: Fri Jun 13, 2008 12:21 pm
Posts: 1
All,

I'm trying to build a simple list based on a lookup table using:
Hibernate 3.2
MySQL 5.0.37
Java 1.6

I tried to use the following code to list all of the members:
List custGroups = session.createQuery("SELECT id,name FROM test.CustomerGroup").list();
session.getTransaction().commit();

System.out.println("list : " + custGroups);

Iterator it = custGroups.iterator();
while (it.hasNext()) {
custGroup = (CustomerGroup) it.next();
//Object obj = it.next();
System.out.println("obj : " + custGroup);
cg = new SelectItem(custGroup,custGroup.getName());
cgs.add(cg);
}

I get a class cast exception. The objects in the list are java.lang.Object, not test.CustomerGroup. The query works:

Hibernate: select customergr0_.id as col_0_0_, customergr0_.custGroup as col_1_0_ from servercustGroup customergr0_
list : [[Ljava.lang.Object;@e06ce, [Ljava.lang.Object;@e0f0ad, [Ljava.lang.Object;@1d8f162, [Ljava.lang.Object;@1cf662f]

There are four results, four entries in the table, but why aren't they being mapped to test.CustGroup?

I have the following config:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/trend102</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->

<mapping file="/home/mlb/apps/tomcat/webapps/jsf-test/WEB-INF/hibernate.xml"/>

</session-factory>

</hibernate-configuration>

and the following mapping:

<hibernate-mapping>
<class name="test.CustomerGroup" table="servercustGroup">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="name" type="string" column="custGroup" />
</class>
</hibernate-mapping>

Any help would be appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 1:05 pm 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
Hi,
Hibernate will return a list of Object[] ( array) ; example

Object[] obj = {1,"name"}


so you returned object is not CustomerGroup but rather Object[]


try with this


Object[] obj = (Object[] ) it.next();
System.out.println("id-->" + obj[0]);
System.out.println("name-->" + obj[1]);


if your query is


SELECT name FROM test.CustomerGroup
then your returned list is a String
which will be

String name = (String ) it.next();

and if your query is like

SELECT id FROM test.CustomerGroup
then the list will be Long datatype
which become

Long id= (Long) it.next();


I hope this could help

_________________
Alan Mehio
London
UK


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 2:58 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Another quick troubleshooting tip might just be to find out what the object type is in that List. So, do something like this inside your looP:

System.out.println(obj[i].getClass());

Then you'll know for sure what the object type is. Then you can begin to handle your data with a bit more knowledge of what Hibernate3 is returning to you.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.