Java RavenDB查询返回null
我在Ravendb3.5.35183上。我有一种类型:Java RavenDB查询返回null,java,ravendb,Java,Ravendb,我在Ravendb3.5.35183上。我有一种类型: import com.mysema.query.annotations.QueryEntity; @QueryEntity public class CountryLayerCount { public String countryName; public int layerCount; } 以及以下查询: private int getCountryLayerCount(String countryName, IDoc
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;
}
当插入浏览器时,它正确地为我提供:
{"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();
CountryLayerCount/ByName
看起来像什么?@Ayende:更新了问题谢谢你提供了一个详细、简洁且非常有用的答案。我的责任可能是没有正确阅读文档和依赖假设。。。
QCountryLayerCount countryLayerCountSurrogate =
QCountryLayerCount.countryLayerCount;
CountryLayerCount levelDepthQuery = currentSession
.query(CountryLayerCount.class, "CountryLayerCount/ByName")
.where(countryLayerCountSurrogate.countryName.eq(countryName))
.single();