I'm getting a ClassCastException when trying to get a "Product" object from my database:
Code:
java.lang.ClassCastException: com.cup.model.Product
at com.cup.model.ProductHome.findById(ProductHome.java:133)
at com.cup.servlet.StartpageController.handleRequest(StartpageController.java:61)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:675)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:623)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:384)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:344)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)
I'm using:
MySQL 5.0
Jboss 4.0.4
Hibernate 3.0
Here are my files:
Product.java
Code:
package com.cup.model;
// Generated 02.jun.2006 22:55:56 by Hibernate Tools 3.1.0.beta5
/**
* Product generated by hbm2java
*/
public class Product implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 6300486666868987926L;
private String id;
private Integer amount;
private Double price;
private String name;
// Constructors
/** default constructor */
public Product() {}
/** minimal constructor */
public Product(String id) {
this.id = id;
}
/** full constructor */
public Product(String id, Integer amount, Double price, String name) {
this.id = id;
this.amount = amount;
this.price = price;
this.name = name;
}
// Property accessors
public String getId()
{
return this.id;
}
public void setId(String id)
{
this.id = id;
}
public Integer getAmount()
{
return this.amount;
}
public void setAmount(Integer amount)
{
this.amount = amount;
}
public Double getPrice()
{
return this.price;
}
public void setPrice(Double price)
{
this.price = price;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
}
Product.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">
<!-- Generated 02.jun.2006 22:55:56 by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
<class name="com.cup.model.Product" table="product">
<id name="id" type="string">
<column name="id" length="32" />
<generator class="assigned" />
</id>
<property name="amount" type="java.lang.Integer">
<column name="amount"></column>
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0"></column>
</property>
<property name="name" type="string">
<column name="name" length="100"></column>
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
Code:
<?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 name="SessionFactory">
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/resultservice</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.username">****</property>
<property name="hibernate.connection.password">*******</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.pool_size">4</property>
<property name="hibernate.show_sql">true</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- mapping files -->
<mapping resource="com/cup/model/Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
Code that fails (from ProductHome.java):
Code:
public Product findById(java.lang.String id)
{
try
{
Product instance = (Product) session.get("com.cup.model.Product", id);
if (instance == null)
{
log.debug("get successful, no instance found");
} else
{
log.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re)
{
log.error("get failed", re);
throw re;
}
}
When debuging I can see that the correct object is returned. When running the getClass() method on the object it even returns "com.cup.model.Product". But why can't I cast the object?
Please help!