postgresql – Spring Data返回List而不是List

postgresql – Spring Data返回List而不是List,第1张

概述我有关于 spring-data的DAO实现: public interface TestDataRepository extends CrudRepository<DpConfigData, Long> {@Query(value = "select distinct(oid) from unit", nativeQuery = true) List<Long> testMethod( 我有关于 spring-data的DAO实现:
public interface TestDataRepository extends CrudRepository<DpConfigData,Long> {@query(value = "select distinct(oID) from unit",nativequery = true)    List<Long> testMethod();}

和单元测试来测试被管理的DAO:

@Testpublic voID test(){    List<Long> testData = dpConfigDataEntityDataRepository.testMethod();    for (Long oID:testData){        System.out.print(oID);    }}

运行测试给出奇怪的结果 – List< Long>运行时的testData由BigInteger实例填充,而不是由Long填充.结果我得到ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long

JPA实现 – Hibernate.
作为DB我使用Postgresql,unit.oID字段在DB层上有BigInt类型.
在获取整个单元的情况下它被映射到Long,但是使用自定义查询作为“select distinct …”时出现了错误并且它被映射到BigInteger.

所以,我的问题是:这种奇怪行为的原因是什么?
如何以优雅的方式解决/解决它?

这是Spring数据JPA的一个问题.
如果在DB中数据类型定义为BigInteger,并且在JPA查询中我们尝试获取为Long,则它不会给出任何错误,但它在Long数据类型中将值设置为BigInteger.

解决方案:

>使用BigInteger作为返回类型

@query(value =“从单元中选择distinct(oID)”,nativequery = true)
列表与LT; BigInteger的>测试方法();

然后将变量设置如下.
Long variable = bigIntegerValue.longValue();
>使用String作为返回类型并转换为Long

@query(value =“从单元中选择distinct(oID)”,nativequery = true)
列表与LT;字符串>测试方法();

然后将值设置为

Long variable = Long.valueOf(stringValue);
>将DB列类型更改为Integer / Number.
>从实体对象中获取值.

Long variable = dpConfigData.getoID();

其中dpConfigData是Entity的对象(DpConfigData.class)

总结

以上是内存溢出为你收集整理的postgresql – Spring Data返回List而不是List全部内容,希望文章能够帮你解决postgresql – Spring Data返回List而不是List所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1181626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存