java - RavenDB 查询返回 null

标签 java ravendb

我使用的是 RavenDB 3.5.35183。我有一个类型:

import com.mysema.query.annotations.QueryEntity;

@QueryEntity
public class CountryLayerCount
{
    public String countryName;
    public int layerCount;
}

以及以下查询:

private int getCountryLayerCount(String countryName, IDocumentSession currentSession)
{           
    QCountryLayerCount countryLayerCountSurrogate = QCountryLayerCount.countryLayerCount;
    IRavenQueryable<CountryLayerCount> levelDepthQuery = currentSession.query(CountryLayerCount.class, "CountryLayerCount/ByName").where(countryLayerCountSurrogate.countryName.eq(countryName));
    CountryLayerCount countryLayerCount = new CountryLayerCount();
    try (CloseableIterator<StreamResult<CountryLayerCount>> results = currentSession.advanced().stream(levelDepthQuery))
    {
        while(results.hasNext())
        {
            StreamResult<CountryLayerCount> srclc = results.next();
            System.out.println(srclc.getKey());
            CountryLayerCount clc = srclc.getDocument();
            countryLayerCount = clc;
            break;
        }
    }
    catch(Exception e)
    {
    }
    return countryLayerCount.layerCount;
}

查询成功执行,并显示我正在检索的文档的正确 ID(例如“CountryLayerCount/123”),但其数据成员均为空。 where 子句也可以正常工作,国家/地区名称用于检索各个国家/地区。这很简单,但我看不出我错在哪里。 StreamResult 包含正确的键,但 getDocument() 不起作用 - 或者更确切地说,它不包含对象。该集合具有字符串 ID。

在数据库记录器中,我可以看到传入的请求:

Receive Request #  29: GET     - geodata - http://localhost:8888/databases/geodata/streams/query/CountryLayerCount/ByName?&query=CountryName:Germany
Request #  29: GET     -    22 ms - geodata    - 200 - http://localhost:8888/databases/geodata/streams/query/CountryLayerCount/ByName?&query=CountryName:Germany

当插入浏览器时,正确地给出:

{"Results":[{"countryName":"Germany","layerCount":5,"@metadata":{"Raven-Entity-Name":"CountryLayerCounts","Raven-Clr-Type":"DbUtilityFunctions.CountryLayerCount, DbUtilityFunctions","@id":"CountryLayerCounts/212","Temp-Index-Score":0.0,"Last-Modified":"2018-02-03T09:41:36.3165473Z","Raven-Last-Modified":"2018-02-03T09:41:36.3165473","@etag":"01000000-0000-008B-0000-0000000000D7","SerializedSizeOnDisk":164}}
]}

索引定义:

from country in docs.CountryLayerCounts
select new {
    CountryName = country.countryName
} 

据我所知,不必索引对象的所有字段即可完整检索它,对吗?换句话说,我只需要索引字段来查找对象,而不是我想要检索的所有字段;至少这是我的理解......

谢谢!

最佳答案

该问题与大小写不正确有关。

例如:

try (IDocumentSession sesion = store.openSession()) {
    CountryLayerCount c1 = new CountryLayerCount();
    c1.layerCount = 5;
    c1.countryName = "Germany";

    sesion.store(c1);
    sesion.saveChanges();
}

另存为:

{
    "LayerCount": 5,
    "CountryName": "Germany"
}

请注意,我们在 json 中使用大写字母作为属性名称(这只适用于 3.X 版本)。

因此,为了使其正常工作,请更新 json 属性名称 + 编辑您的索引:

from country in docs.CountryLayerCounts
select new {
    CountryName = country.CountryName
} 

顺便说一句。如果您有按国家/地区聚合,那么您可以简单地使用以下方式进行查询:

QCountryLayerCount countryLayerCountSurrogate = 
QCountryLayerCount.countryLayerCount;
    CountryLayerCount levelDepthQuery = currentSession
            .query(CountryLayerCount.class, "CountryLayerCount/ByName")
            .where(countryLayerCountSurrogate.countryName.eq(countryName))
            .single();

关于java - RavenDB 查询返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48597631/

相关文章:

RavenDB : Storage Size Problems

RavenDB 升级

ravendb - 我可以在 javascript SPA 中使用 ravendb

java - 如何在 Java 中使用数组的一部分初始化列表

Java:在 Apache Commons Lang 3.3.2 中找不到 IntRange

java - 如何将从 Web 服务接收到的 Binary64 格式的图像转换为实际图像?

java - 代理对象抛出 NullPointerException

file-upload - 使用 RavenDB 存储上传的文件数据

recursion - RavenDB 和分层文档

java - 在 JavaFX 中为 Canvas 创建一个 "shadow"层?