当前位置: 代码迷 >> J2EE >> java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Inte,该如何处理
  详细解决方案

java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Inte,该如何处理

热度:8109   发布时间:2013-02-25 21:39:50.0
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Inte
package card.struts;

import global.hibernate.MyBeanFactory;
import history.hibernate.History;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import allDao.CardDao;
import allDao.HistoryDao;
import card.hibernate.Card;

import com.opensymphony.xwork2.ActionSupport;

public class ChoiseCardAction extends ActionSupport implements SessionAware{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private int cardId;
private CardDao cardDao;
private HistoryDao historyDao;
@SuppressWarnings("rawtypes")
private Map session;

@SuppressWarnings("rawtypes")
public Map getSession() {
return session;
}

public void setSession(@SuppressWarnings("rawtypes") Map session) {
this.session = session;
}
public int getCardId() {
return cardId;
}

public void setCardId(int cardId) {
this.cardId = cardId;
}

@SuppressWarnings("unchecked")
public String execute()
{
System.out.println("正在执行choiseCard请求!");
if(this.cardDao==null)
{
this.cardDao=(CardDao)MyBeanFactory.MyGetBean("cardDao");
}
if(this.historyDao==null)
{
this.historyDao=(HistoryDao)MyBeanFactory.MyGetBean("historyDao");
}
Card card=this.cardDao.queryById(this.cardId);
System.out.println("选择的卡号为:"+cardId);
System.out.println("卡里还有的钱:"+card.getBalance());
this.session.put("card",card);
List<History> listOfHistory=this.historyDao.queryByCard(this.cardId);//问题就在这里
System.out.println("此卡共有消费记录条数:"+listOfHistory.size());
this.session.put("listOfHistory", listOfHistory);
return "success";
}
}

下面是控制台的输出:
正在执行choiseCard请求!
选择的卡号为:7951
卡里还有的钱:100.0

问题是listOfHistory=this.historyDao.queryByCard(this.cardId);中如果我把this.cardId改成7951就能运行通过,但是用this.cardId就会报错:

java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer
org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:36)
org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:57)
org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92)
org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:305)
org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:300)
org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:57)
org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2602)
org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2983)
org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2918)
org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3247)
org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:140)
org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:276)
org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
  相关解决方案