[英]expected type: java.lang.Double actual value: java.lang.Integer error in hibernate
I call the stored procedure in hibernate using createSqlQuery interface and i am using result transformer for getting the result as bean.我使用 createSqlQuery 接口在 hibernate 中调用存储过程,并且我使用结果转换器将结果作为 bean 获取。 below is my code for that
下面是我的代码
Query query=sessionfactory.getCurrentSession().createSQLQuery("exec usp_ListUsersNotSubmitTimesheet :startDate,:endDate");
query.setDate("startDate", formatter.parse(startDate));
query.setDate("endDate", formatter.parse(endDate));
query.setResultTransformer(Transformers.aliasToBean(UnSubmittedTimesheetBean.class));
List queryList=query.list();
Some times sp result returns all integer values some times decimal values sometimes fixed.so i can't able to give the specific datatype.有时 sp 结果返回所有整数值有时是十进制值有时是固定的。所以我无法给出特定的数据类型。 so i am getting the below exception
所以我收到以下异常
expected type: java.lang.Double actual value: java.lang.Integer
How to resolve this error?如何解决此错误?
Any help will be greatly appreciated!!!任何帮助将不胜感激!!!
Your Stored Procedure is returning Integer data type value and I guess in your bean the mapped column is defined as Double so when Transformer is going to set the results in Bean it is trying to set Integer value to Double value and there it is throwing exception...您的存储过程正在返回 Integer 数据类型值,我猜在您的 bean 中映射列被定义为 Double,因此当 Transformer 将在 Bean 中设置结果时,它试图将 Integer 值设置为 Double 值,并在那里抛出异常。 ..
1> You can Either made changes in your Bean class (can make column as Integer). 1> 您可以在 Bean 类中进行更改(可以将列设为整数)。
2> you can change returned type of procedure that will return Double Precision. 2> 您可以更改将返回双精度的过程的返回类型。
3> and the complicated one of you are not allowed to change any of the above You can write your own Result Transformer by Extending "AbstractQueryTransformer". 3> 和复杂的你不能改变以上任何一个你可以通过扩展“AbstractQueryTransformer”来编写你自己的Result Transformer。
i prefer the second one.我更喜欢第二个。
My solution is just add casting in your stored procedure as what int / decimal我的解决方案只是在您的存储过程中添加转换为 int/decimal
So that you can easily handle the datatype in your result transformer bean.这样您就可以轻松处理结果转换器 bean 中的数据类型。 Otherwise in you result transformer make all those fields as "Number" and then change the "Number" object to any other things based on your need.
否则,结果转换器将所有这些字段都设置为“数字”,然后根据需要将“数字”对象更改为任何其他内容。
Fastest solution, change the stored procedure so that the result is always a double precision number, regardless of anything (you'll need to change the sp code, maybe the return type, as I don't know your sp).最快的解决方案,更改存储过程,以便结果始终是双精度数,无论什么(您需要更改 sp 代码,可能是返回类型,因为我不知道您的 sp)。 Solution if you can't touch the db: Build a result transformer to accept integer, float, whatever and accomodate it into a double after doing some processing.
如果您无法触摸 db 的解决方案:构建一个结果转换器以接受整数、浮点数等,并在进行一些处理后将其容纳为双精度数。
I faced similar with complex SQL Joins for 2 fields.我遇到了类似的 2 个字段的复杂 SQL 连接。 The exception shown on the console was
控制台上显示的异常是
expected type: java.lang.Double actual value: java.math.BigDecimal
预期类型:java.lang.Double 实际值:java.math.BigDecimal
To solve this I have explicitly mapped the datatype for those fields using addScalar() method为了解决这个问题,我使用 addScalar() 方法显式映射了这些字段的数据类型
session.createSQLQuery(query.toString())
.addScalar("offerid", StandardBasicTypes.INTEGER)
.addScalar("offervalue", StandardBasicTypes.DOUBLE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.