1

ジェネリックオブジェクトのリストを返すクエリを作成しようとしています。Java Spring、MongoTemplateとジェネリックタイプで探す

それは私のベースオブジェクトです:

@Getter 
@Setter 
@EqualsAndHashCode(of = {"id"}) 
public class Normalized { 

    public static final String J_ID = "id"; 
    public static final String J_CREATED_AT = "createdAt"; 
    public static final String J_UPDATED_AT = "udpatedAt"; 

    @Id 
    @Indexed 
    @JsonProperty(J_ID) 
    private String id; 
    @JsonProperty(J_CREATED_AT) 
    private Instant createdAt; 
    @JsonProperty(J_UPDATED_AT) 
    private Instant updatedAt; 

} 

それは私のドメインオブジェクトです:

@Data 
@Builder 
@NoArgsConstructor 
@AllArgsConstructor 
@EqualsAndHashCode(callSuper = true) 
@JsonInclude(JsonInclude.Include.NON_NULL) 
@Document  
public class NormalizedContent<E extends OriginalContent> extends Normalized { 

    public static final String J_SOURCE = "source"; 
    public static final String J_EXTERNAL_URL = "externalUrl"; 
    public static final String J_MAIN_TEXT = "mainText"; 
    public static final String J_ORIGINAL_CONTENT = "originalContent"; 
    public static final String J_CREATED_DATE = "createdDate"; 

    @Indexed 
    @JsonProperty(J_SOURCE) 
    private Sources source; 

    @Indexed 
    @JsonProperty(J_CREATED_DATE) 
    private Instant createdDate; 

    @JsonProperty(J_EXTERNAL_URL) 
    private String externalUrl; 

    @JsonProperty(J_MAIN_TEXT) 
    private String mainText; 

    @JsonProperty(J_ORIGINAL_CONTENT) 
    private E originalContent; 
} 

そしてそれは私の機能の簡素化です:

public Page<NormalizedContent<? extends OriginalContent>> findAllByQueriesAndFilters(
     List<String> queriesId, Pageable pageable) { 
    if (queriesId == null || queriesId.isEmpty()) { 
     throw new IllegalArgumentException("Queries unspecified"); 
    } 
    Criteria criteria = Criteria.where(NormalizedContent.J_QUERY).in(queriesId); 
    Query query = new Query(criteria); 
    query.with(pageable); 
    List<NormalizedContent<? extends OriginalContent>> content = 
     mongoTemplate.find(query, NormalizedContent.class); 

    Long total = mongoTemplate.count(query, OTBUserpanelUser.class); 

    return new PageImpl<NormalizedContent<? extends OriginalContent>>(content, pageable, total); 
    } 

問題は行にあります。

List<NormalizedContent<? extends OriginalContent>> content = 
      mongoTemplate.find(query, NormalizedContent.class); 

コンテンツ変数がNormalizedContent<? extends OriginalContent>のリストであり、findメソッドがNormalizedContentのリストを返すためです。

findメソッドがNormalizedContent<? extends OriginalContent>のリストを返すにはどうすればよいですか?コンテンツからNormalizedContentを引退しながら

+0

'(Class >)NormalizedContent.class'をキャストしようとしましたか? –

+0

@OrestKyrylchukこれは私の現在の解決策ですが、このためには、リストからすべての要素を1つずつマップする必要があります。 findメソッドがこれを返すようにしたいのですが、 'NormalizedContent <? extends OriginalContent> .class' –

+0

次のコードは 'List > Context >> Context >> content = mongoTemplate.find(query、(Class >)あなたは何を意味するのかわからない –

答えて

-1
List<NormalizedContent> content = 
    mongoTemplate.find(query, NormalizedContent.class); 

は、<? extends OriginalContent>タイプを保持thetheプロパティにキャスト。

+0

[How to answer](https://stackoverflow.com/help/how-to-answer)を見て、答えを更新して詳細を提供してください。具体的には、これがどのように問題を解決するかを説明した場合に役立ちます – Ortund

関連する問題