Ignite Spring Data @Query注释是否支持投影概念来检索字段的子集而不是整个对象?

问题描述 投票:0回答:1

我想知道Apache Ignite Spring Data实现是否支持projections的概念,以便于仅检索缓存实体的字段/属性的子集,而不是获取可能包含大量列的整个实体。

在文档中,我看到一个示例,其中只提取缓存实体的一个属性,如下所示。

 /**
 * Getting ids of all the Person satisfying the custom query from {@link Query} annotation.
 *
 * @param orgId Query parameter.
 * @param pageable Pageable interface.
 * @return A list of Persons' ids.
 */
@Query("SELECT id FROM Person WHERE orgId > ?")
public List<Long> selectId(long orgId, Pageable pageable);

以上面的例子为例,如果一个人需要“人”实体的“id”和“firstName”,“lastName”而不是上面的“id”,怎么做?

谢谢

更新(发布@alamar的答案):谢谢@alamar!我还从Spring Data Test代码中复制方法声明及其使用代码,以便其他人可以轻松地回答。

/** */
@Query("SELECT _key, secondName FROM Person WHERE firstName REGEXP ?")
public List<List> selectSeveralField(String val, Pageable pageable);

/** */
public void testSelectSeveralFields() {
    List<List> lists = repo.selectSeveralField("^[a-z]+$", new PageRequest(2, 6));

    assertEquals(6, lists.size());

    for (List list : lists) {
        assertEquals(2, list.size());

        assertTrue(list.get(0) instanceof Integer);
    }
}

但是,使用Spring Data Projections类型的机制可以消除用户代码中令人讨厌的强制转换。

spring-data ignite gridgain
1个回答
1
投票

从Spring Data测试:

@Query("SELECT _key, secondName FROM Person WHERE firstName REGEXP ?")
public List<List> selectSeveralField(String val, Pageable pageable);

例如。是的,您可以列出多个字段并获取这些字段的列表(元组)列表。

© www.soinside.com 2019 - 2024. All rights reserved.