English 中文(简体)
获得实体键
原标题:Getting an Entity key

Based on the Q&A from here: Get an Objectify Entity s Key

对于持续对象,获得实体的钥匙如下:

@Transient
Key<Categoria> getKey() {
   return new Key<Categoria>(Categoria.class, id);
}

Doenn t 返回相同的密钥 :

Objectify ofy = ObjectifyService.begin();
Key<Categoria> key = ofy.getFactory().getKey(someobject);

还是应该?

我的模特看起来是这样的:

@Entity
class Categoria{
  @Parent
  private Key<Someclass> parentKey;

  @Transient
  Key<Categoria> getKey() {
   return new Key<Categoria>(Categoria.class, id);
 }
 // Code omitted
}
最佳回答

只有 Categoria Parent 字段时,它才会产生不同的密钥。在这种情况下,您需要将父密钥与类和代号一起传送到密钥构建器中。

问题回答

它应该。 我总是通过从长代号上创建的密钥来获取目标实体的密钥。 您也可以使用返回的密钥在需要时从密钥上获取长代号 。

EDIT: You can t get the key the way you are trying.

你必须这样做。

Key<Car> rootKey = new Key<Car>(Car.class, 959);
Key<Car> keyWithParent = new Key<Car>(parent, Car.class, 959);

"http://code.google.com/p/objectify-appengine/wiki/Conceptps#Keys" rel="nofollow" 来源于

So for this line: Key key = ofy.getFactory().getKey(someobject);

键将包含父键PLUS Categoria 键

,这意味着当您在函数中查找时,您必须包含父密钥

Key<Categoria> getKey() {
 return new Key<Categoria>(parentKey, Categoria.class, id);
}




相关问题
How to make logging.debug work on Appengine?

I m having a tough time getting the logging on Appengine working. the statement import logging is flagged as an unrecognized import in my PyDev Appengine project. I suspected that this was just an ...

gqlQuery returns object, want list of keys

Is there a way to convert the GqlQuery object to an array of keys, or is there a way to force the query to return an array of keys? For example: items = db.GqlQuery("SELECT __key__ FROM Items") ...

Integrating Google AppEngine with a Thick Client

I want to make a multi-user client-server solution with Java Swing thick client as a front-end and Google AppEngine (Java one) as a back-end. The problem is that GAE provides only web-based forms for ...

sorl.thumbnail : thumbnail is not a valid tag library?

I am trying to install sorl.thumbnail but am getting the following error message: thumbnail is not a valid tag library: Could not load template library from django.templatetags.thumbnail, No module ...

热门标签