RavenDB查询返回null

时间:2018-02-03 12:59:48

标签: 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(例如&#34; CountryLayerCount / 123&#34;),但其数据成员均为空。 where子句也可以正常工作,国家/地区名称用于检索单个国家/地区。这很简单,但我无法看到我出错的地方。 StreamResult包含正确的键,但getDocument()不起作用 - 或者更确切地说,它不包含对象。该集合具有字符串ID。

在db logger中,我可以看到请求进入:

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
} 

AFAIK,人们不必索引对象的所有字段来完整地检索它,对吧?换句话说,我只需要索引字段以找到对象,而不是我想要检索的所有字段;至少那是我的理解......

谢谢!

1 个答案:

答案 0 :(得分:2)

问题与套管不正确有关。

例如:

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();