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
|