int Serializable导致的Provided id of the wrong type. Expected异常

本文探讨了在Java方法参数传递过程中遇到的序列化异常问题,并提供了将int类型转换为Integer类型的解决方案,以确保参数正确传递并避免异常发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int类型是不可序列化的,参数传递时,如果传过去后的参数是Serializable,则必须把int型转换为Integer类型如:

public void deleteStation(Integer[] ids) {
   try {
    stationParDAO.delete(stuIfno.class, ids);
   } catch (DAOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

}

被调用的方法: delete(Class<T> refClass, Serializable[] key)
如果上例中主调方法改为:
public void deleteStation(int[] ids) {
   try {
    stationParDAO.delete(stuIfno.class, ids);
   } catch (DAOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
}

异常如下:
javax.servlet.ServletException: org.springframework.orm.hibernate3.HibernateSystemException: Provided id of the wrong type. Expected: class java.lang.Integer, got class [I; nested exception is org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Integer, got class [I
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)

root cause

org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Integer, got class [I org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86) org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878) org.hibernate.impl.SessionImpl.get(SessionImpl.java:815) org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值