简体   繁体   English

为什么在尝试使用参数化的泛型获取查询时出现空列表

[英]Why I am getting the empty list while trying to fetch the query with the paramaterized generic

I am trying to fetch the List of the Type as depicted in the code below -: 我正在尝试获取以下代码中所示的类型列表-:

    @Autowired
    MongoTemplate mongotemplate;

    @Override
    public <T> List<T> getDetails(Query query, Class<T> queryClass) {

        List<T>  list= mongotemplate.find(query, queryClass);

    return list;
    }

}

This repository is called from the service as-: 该存储库从服务中调用为:

return messageSummaryImpl.getDetails(query, MessageSummaryDoc.class);

and the return Type of this function is List<MessageSummaryDoc> 并且此函数的返回类型为List<MessageSummaryDoc>

As the result of this is an Empty list,This code works fine when i dont use the generic function or say by directly calling the repository function from the service itself (mongorepository.find(query,Classexample.class)) Please suggest me what i am doing wrong. 由于这是一个空列表,当我不使用泛型函数或通过直接从服务本身调用存储库函数(mongorepository.find(query,Classexample.class))说时,此代码可以正常工作(mongorepository.find(query,Classexample.class))请告诉我做错了。

PS-: please don't ask me why i am not calling the repository directly(As this is the project requirement). PS-:请不要问我为什么我不直接调用存储库(因为这是项目要求)。

Either you have no entires that matches query or your mongotemplate does something fancy with query. 您可能没有匹配查询的整体,或者您的mongotemplate对查询做了一些幻想。

You stated that if you do (mongorepository.find(query,Classexample.class)) you got some data, but you are not using mongorepository in given code but mongotemplate so that is "false proof of concept" 你说,如果你这样做(mongorepository.find(query,Classexample.class))你得到了一些数据,但你不使用mongorepository在给定的代码,但mongotemplate所以这是“概念的假证明”

Generics won't affect this issue by any means - they are gone on runtime anyway. 泛型不会以任何方式影响此问题-无论如何它们都在运行时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试合并两个排序的链接列表时,为什么会得到一个空列表? - why am I getting an empty list when I am trying to merge two Sorted Linkedlists? 为什么我在尝试更改代码时收到错误消息? - Why am I getting an error message while trying to code for change? 为什么在尝试将实体保存到列表时会收到 NullPointerException? - Why am i getting a NullPointerException when trying to save an entity to a list? 为什么我在尝试反序列化列表时收到 ClassCastException - Why am I getting ClassCastException when trying to deserialize a list 我在尝试通过 URL 直接从表中获取数据时遇到 SQLGrammarException - I am getting a SQLGrammarException while trying to fetch data from table directly through URL 我正在尝试返回一个空列表 - I am trying to return an empty list Hibernate:为什么我得到一个空的 json? - Hibernate: Why am I getting an empty json? 在检查数组字符串是否为空时为什么会出现nullpointer异常? - Why am I getting a nullpointer exception while I'm checking whether an array string is empty? 为什么我得到空阵列? - Why am I getting empty arrays? 为什么我在创建自己的异常时会得到一个空的堆栈跟踪? - Why am I getting an empty stacktrace while creating my own exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM