java.lang.Long cannot be cast to java.lang.Integer

在底层dao自己添加id=1
clipboard.png

报错:java.lang.Long cannot be cast to java.lang.Integer
id对应的数据库的id,是不是这里定义为long,数据库的id不能用int啊!
不知道如何去排查这个错误!

还发现:数据库都查询了

DetachableCriteria(CriteriaImpl(com.pdcss.dbzx.survey.entity.SurveyProblems:this[][id=1]))
Hibernate: select this_.id as id1_10_0_, this_.analysis as analysis2_10_0_, this_.answer as answer3_10_0_, this_.bank_id as bank_id4_10_0_, this_.createtime as createti5_10_0_, this_.img as img6_10_0_, this_.problem_name as problem_7_10_0_, this_.type as type8_10_0_, this_.typeStr as typeStr9_10_0_ from survey_problems this_ where this_.id=?

但是为什么会报错?

clipboard.png

已经解决,数据库改成bigint,跟long对应,就ok了!

阅读 5.2k
3 个回答

你使用的是什么数据库?
你把参数(long id)换成(Integer id)。
报这个错的原因的类型转换失败。
long类型无法转换成Integer类型。

可以将id在方法里面转为Integer后在放入dc里面,

String str = id.toString();
Integer id1 = Integer.valueOf(str);
或者Integer id1 = new Integer(str);

实在想强转的话,先把long转成string,然后stringinteger

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题