当前位置: 代码迷 >> Java Web开发 >> java类型异常java.lang.Integer cannot be cast to java.lang.String
  详细解决方案

java类型异常java.lang.Integer cannot be cast to java.lang.String

热度:6202   发布时间:2013-02-25 21:09:36.0
java类型错误java.lang.Integer cannot be cast to java.lang.String
spring framwork DAO的基类(下面代码忽略注解,那个应该没错)
Java code
public class HibernateCommonImpDAO<T extends Serializable, ID extends Serializable>extends HibernateDaoSupport {    [color=#FF0000]public ID save(T transientObject) {        return (ID) super.getSession().save(transientObject);[/color]    }}

这个返回类型是ID 我在这里很没搞懂这是个什么类型,接下来用到save方法的时候怎么定义类型。。。。

Service类的saveUser方法
Java code
public class LoginServiceImp implements LoginService    public String saveUser(LoginResource loginResource){        [color=#FF0000]return (String)loginResourceDAO.save(loginResource);[/color]           }}

LoginResource 是model实体
这里的类型就出问题啦。。。。
Java code
@RequestMapping(value="/regeditSave.do")    public String regeditSave(HttpServletRequest request,Model model) throws Exception {        HttpSession session = request.getSession();        try        {            LoginResource user = new LoginResource();            [color=#FF0000]loginService.saveUser(user);[/color]            session.setAttribute("sessionUser", user);                    }        catch(Exception e)        {            e.printStackTrace();        }                return "welcome";//跳转到欢迎界面        }

页面结果:页面跳转了,但添加不进数据库,session为空。
异常:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.sofmit.sd.login.service.LoginServiceImp.saveUser(LoginServiceImp.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy7.saveUser(Unknown Source)
at com.sofmit.sd.login.web.LoginController.regeditSave(LoginController.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
  相关解决方案