gpt4 book ai didi

java - spring-data-aerospike `findAll(ids)` 结果返回 NULL

转载 作者:行者123 更新时间:2023-11-29 07:25:59 32 4
gpt4 key购买 nike

Spring-data-aerospike方法 findAll(Iterable<ID> ids)返回列表 NULL s 表示不存在的实体。

如果某些实体确实存在而其中一些存在-结果将是IterableNULL s 和现有实体组合:[entity1, null, entity2, null]

在处理 findAll(Iterable<ID> ids) 的结果时没有用, 因为 NPE秒。

查看源代码后,我在类 org.springframework.data.aerospike.core.AerospikeTemplate 中找到了以下方法:

@Override
public <T> List<T> findByIDs(Iterable<Serializable> IDs, Class<T> type){
AerospikePersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
List<Key> kList = new ArrayList<Key>();
IDs.forEach(id -> kList.add(new Key(this.namespace, entity.getSetName(), id.toString())));
Record[] rs = this.client.get(null, kList.toArray(new Key[kList.size()]));
final List<T> tList = new ArrayList<T>();
for(int i=0; i < rs.length; i++)
tList.add(mapToEntity(kList.get(i), type, rs[i])); // <--- mapToEntity here may return NULL (is it a bug or by design?)
return tList;
}

因此,问题是:

  1. 这是设计的正常行为吗?
  2. 不应该是这样的:if(entity != null) tList.add(entity);相反?

更新。

确保预期行为返回NULL我研究了 org.springframework.data.jpa.repository.JpaRepository 的实现(特别是一个类 org.springframework.data.jpa.repository.support.SimpleJpaRepository )并找到了关于我的案例的以下代码:

public List<T> findAllById(Iterable<ID> ids) {

Assert.notNull(ids, "The given Iterable of Id's must not be null!");

if (!ids.iterator().hasNext()) {
return Collections.emptyList();
}

if (entityInformation.hasCompositeId()) {

List<T> results = new ArrayList<T>();

for (ID id : ids) {
findById(id).ifPresent(results::add); // <--- here is what I meant above - we add only existing entities
}

return results;
}

ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
TypedQuery<T> query = getQuery(specification, Sort.unsorted());

return query.setParameter(specification.parameter, ids).getResultList();
}

最佳答案

问题是 fixed在最新的快照中。

只要 Aerospike 团队的 smbdy 提供了新版本——您就可以使用它。到目前为止,您应该过滤空值。这令人沮丧,我同意 :)

关于java - spring-data-aerospike `findAll(ids)` 结果返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52702531/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com