[英]Spring data jpa, Native Query, returned wrong field types
I have following native query method in my repository: 我的存储库中有以下本机查询方法:
@Query(value="SELECT appSub.ApplicationFormId as appFormId, appSub.profileId as profileId, "
+ "p.CASId as profileCASId, ps.programId as programId FROM [unicas_config].[dbo].ApplicationFormEarlyDecisionConfig appFormED "
+ "INNER JOIN [unicas_ux].[dbo].ApplicationSubmission appSub ON appFormED.ApplicationFormId = appSub.applicationFormId "
+ "INNER JOIN [unicas_ux].[dbo].Profile p ON appSub.profileId = p.id "
+ "INNER JOIN [unicas_ux].[dbo].ProgramSelected ps ON p.id=ps.ProfileId AND appSub.applicationFormId = ps.instanceId "
+ "WHERE appFormED.EarlyDecisionVerdictDate >=:fromDate AND appFormED.EarlyDecisionVerdictDate <:toDate "
+ "AND appSub.EarlyDecisionStatus='Applied Early Decision' "
+ "AND appSub.ApplicationStatus='Received' "
+ "AND ps.IsPaid =1 "
+ "ORDER BY appSub.ApplicationFormId",nativeQuery = true)
List<Object[]> getAllEarlyDecisionApplicantsWithPaidProgramsOnVerdictDate(@Param("fromDate") Date fromDate, @Param("toDate") Date toDate);
Now, I want to map the returned result: 现在,我想映射返回的结果:
long appFormId = (Long)obj[0]
long profileId = (Long)obj[1]
long programId = (Long)obj[3]
When I am doing that, I am getting java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
as Hibernate consider these ids of Integer
type instead of Long
. 当我这样做时,我得到了
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
因为Hibernate认为这些ID是Integer
类型而不是Long
。
Please, tell me how could I programatically tell Hibernate to return proper type. 请告诉我如何以编程方式告诉Hibernate返回正确的类型。
To be on the safe side, I always cast numeric types to Number
and then get the value of desired type from it, as JDBC driver can return Integer
, Long
, BigDecimal
, etc. depending on the type of the database column: 为了安全起见,我总是将数字类型转换为
Number
,然后从中获取所需类型的值,因为JDBC驱动程序可以根据数据库列的类型返回Integer
, Long
, BigDecimal
等:
((Number) obj[0]).longValue()
Of course, don't forget to check for null
if column is nullable. 当然,如果column可为
null
,请不要忘记检查null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.