使用 Objectify 在 Google CloudDatastore 中查找记录
findRecord in Google CloudDatastore with Objectify
我想使用 Objectify 查询 Google Cloud Datastore。基于已知键值对查找记录的合适方法是什么?记录在数据库中,我通过 Google 的数据存储查看器验证了这一点。
这是我的方法存根,它触发了 NotFoundException:
@ApiMethod(name="getUser")
public User getUser() throws NotFoundException {
String filterKey = "googleId";
String filterVal = "jochen.bauer@gmail.com";
User user = OfyService.ofy().load().type(User.class).filter(filterKey, filterVal).first().now();
if (user == null) {
throw new NotFoundException("User Record does not exist");
}
return user;
}
这是用户 class:
@Entity
public class User {
@Id
Long id;
private HealthVault healthVault;
private String googleId;
public User(String googleId){
this.googleId = googleId;
this.healthVault = new HealthVault();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public HealthVault getHealthVault() {
return healthVault;
}
public void setHealthVault(HealthVault healthVault) {
this.healthVault = healthVault;
}
public String getGoogleId() {
return googleId;
}
public void setGoogleId(String googleId) {
this.googleId = googleId;
}
}
我认为它失败是因为交易。您需要进行无交易调用,例如:
User user = OfyService.ofy().transactionless().load().type(User.class).filter(filterKey, filterVal).first().now();
有关 App Engine 交易的更多信息:
https://cloud.google.com/appengine/docs/java/datastore/transactions
https://github.com/objectify/objectify/wiki/Transactions
编辑
您的对象需要 @Index 注释。它将字段添加到数据存储索引。只能搜索索引中的属性。过滤法就是其中之一。
@Id
Long id;
@Index
private HealthVault healthVault;
@Index
private String googleId;
P.S。使用 googleId jochen.bauer@gmail.com 删除您的对象,并在更新您的实体后将其再次写入数据库。 objectify 会找到它。
首先在您的字段模型中添加 @Index
。我没有在您的模型中看到 filterVal
作为电子邮件。即便如此,要获得基于您的 filterVal
的实体,假设 googleId
是您实体的字段。
User user = OfyService.ofy().load().type(User.class).filter("googleId", filterVal).now();
因此,如果您的 filterKey
是您实体的 ID。
User user = OfyService.ofy().load().key(Key.create(User.class, filterKey)).now();
我想使用 Objectify 查询 Google Cloud Datastore。基于已知键值对查找记录的合适方法是什么?记录在数据库中,我通过 Google 的数据存储查看器验证了这一点。
这是我的方法存根,它触发了 NotFoundException:
@ApiMethod(name="getUser")
public User getUser() throws NotFoundException {
String filterKey = "googleId";
String filterVal = "jochen.bauer@gmail.com";
User user = OfyService.ofy().load().type(User.class).filter(filterKey, filterVal).first().now();
if (user == null) {
throw new NotFoundException("User Record does not exist");
}
return user;
}
这是用户 class:
@Entity
public class User {
@Id
Long id;
private HealthVault healthVault;
private String googleId;
public User(String googleId){
this.googleId = googleId;
this.healthVault = new HealthVault();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public HealthVault getHealthVault() {
return healthVault;
}
public void setHealthVault(HealthVault healthVault) {
this.healthVault = healthVault;
}
public String getGoogleId() {
return googleId;
}
public void setGoogleId(String googleId) {
this.googleId = googleId;
}
}
我认为它失败是因为交易。您需要进行无交易调用,例如:
User user = OfyService.ofy().transactionless().load().type(User.class).filter(filterKey, filterVal).first().now();
有关 App Engine 交易的更多信息: https://cloud.google.com/appengine/docs/java/datastore/transactions https://github.com/objectify/objectify/wiki/Transactions
编辑 您的对象需要 @Index 注释。它将字段添加到数据存储索引。只能搜索索引中的属性。过滤法就是其中之一。
@Id
Long id;
@Index
private HealthVault healthVault;
@Index
private String googleId;
P.S。使用 googleId jochen.bauer@gmail.com 删除您的对象,并在更新您的实体后将其再次写入数据库。 objectify 会找到它。
首先在您的字段模型中添加 @Index
。我没有在您的模型中看到 filterVal
作为电子邮件。即便如此,要获得基于您的 filterVal
的实体,假设 googleId
是您实体的字段。
User user = OfyService.ofy().load().type(User.class).filter("googleId", filterVal).now();
因此,如果您的 filterKey
是您实体的 ID。
User user = OfyService.ofy().load().key(Key.create(User.class, filterKey)).now();