Java 使用Objectify在Google CloudDatastore中查找记录

Java 使用Objectify在Google CloudDatastore中查找记录,java,google-app-engine,google-cloud-datastore,objectify,Java,Google App Engine,Google Cloud Datastore,Objectify,我想使用Objectify查询Google云数据存储。基于已知的键值对查找记录的合适方法是什么?记录在数据库中,我通过谷歌的数据存储查看器验证了这一点 这是我的方法存根,它触发NotFoundException: @ApiMethod(name="getUser") public User getUser() throws NotFoundException { String filterKey = "googleId"; String filterVal = "jochen.b

我想使用Objectify查询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;
}
以下是用户类:

@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上交易的更多信息:

编辑 您的对象需要@Index注释。它将把字段添加到数据存储索引中。只能搜索索引中的属性。过滤法就是其中之一

@Id
Long id;
@Index
private HealthVault healthVault;
@Index
private String googleId;

另外,用谷歌ID jochen删除你的对象。bauer@gmail.com并在更新实体后将其再次写入数据库。objectify将找到它。

首先在字段模型中添加
@Index
。在您的型号中,我没有将
filterVal
视为电子邮件。即便如此,要在您的
filterVal
中获取实体,假设是
googleId
是实体的字段

User user = OfyService.ofy().load().type(User.class).filter("googleId", filterVal).now();
User user = OfyService.ofy().load().key(Key.create(User.class, filterKey)).now();
因此,如果您的
filterKey
是实体的id

User user = OfyService.ofy().load().type(User.class).filter("googleId", filterVal).now();
User user = OfyService.ofy().load().key(Key.create(User.class, filterKey)).now();

感谢您的回答-遗憾的是,将transactionless()添加到我的查询中并不能解决此问题。显示您的用户对象。谢谢-关键是删除旧实体!