在我的模型中
@Entity
public class Ent implements Serializable {
@Id
private long key;
@Parent
private Key<Ent> parentKey;
}
创建一个简单层次结构的实体(在我的例子中实际上只有 2 层)。我想仅过滤层次结构的根,这是通过没有父级来定义的 (
parentKey = null
)。
我尝试在 Objectify 中使用
filter()
,但出现异常
java.lang.IllegalArgumentException: @Parent fields cannot be filtered on. Perhaps you wish to use filterKey() or ancestor() instead?
然后我尝试使用
ancestor()
,但它似乎不支持null
值:
javax.servlet.ServletException: java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "pojo" is null
at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:117)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
at org.eclipse.jetty.server.handler.SizeLimitHandler.handle(SizeLimitHandler.java:96)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
at org.eclipse.jetty.server.Server.handle(Server.java:516)
at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487)
at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479)
at com.google.apphosting.runtime.jetty9.RpcConnection.handle(RpcConnection.java:269)
at com.google.apphosting.runtime.jetty9.RpcConnector.serviceRequest(RpcConnector.java:100)
at com.google.apphosting.runtime.jetty9.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:184)
at com.google.apphosting.runtime.RequestRunner.dispatchServletRequest(RequestRunner.java:262)
at com.google.apphosting.runtime.RequestRunner.dispatchRequest(RequestRunner.java:227)
at com.google.apphosting.runtime.RequestRunner.run(RequestRunner.java:193)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:273)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "pojo" is null
at com.googlecode.objectify.impl.Keys.getMetadataSafe(Keys.java:70)
at com.googlecode.objectify.impl.Keys.rawKeyOf(Keys.java:52)
at com.googlecode.objectify.impl.Keys.anythingToRawKey(Keys.java:122)
at com.googlecode.objectify.impl.QueryImpl.setAncestor(QueryImpl.java:221)
at com.googlecode.objectify.impl.SimpleQueryImpl.ancestor(SimpleQueryImpl.java:69)
at
我想我应该使用
filterKey()
,问题是如何创建一个“过滤键”来接受任何id
,但只接受null
父级...
我不想告诉你坏消息,但我认为你不能。没有“没有祖先的实体”的索引。这只是不是数据存储操作。
我会向您的实体添加一个索引字段以启用您想要的查询。您可能需要在所有实体之间映射/减少,重新保存它们以添加字段。