1

Google Cloud DatastoreのクエリにObjectifyを使用したいと考えています。既知のキーと値のペアに基づいてレコードを見つける適切な方法は何ですか?レコードがデータベースにあり、GoogleのDatastoreビューアでこれを確認しました。ここでGoogle CloudDatastoreのfindRecordとObjectify

はNotFoundExceptionをトリガ私のメソッドスタブ、次のとおりです。ここで

@ApiMethod(name="getUser") 
public User getUser() throws NotFoundException { 
    String filterKey = "googleId"; 
    String filterVal = "[email protected]"; 
    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; 
} 

は、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; 
} 
} 

答えて

1

私はそれがあるため、取引の失敗だと思います。 App Engineの上のトランザクションに関する

User user = OfyService.ofy().transactionless().load().type(User.class).filter(filterKey, filterVal).first().now(); 

詳細情報::あなたのオブジェクトは@Index注釈を必要と https://cloud.google.com/appengine/docs/java/datastore/transactions https://github.com/objectify/objectify/wiki/Transactions

EDITあなたは次のようtransctionless呼び出しを行う必要があります。これは、データストアインデックスにフィールドを追加します。インデックスにあるプロパティのみを検索できます。フィルター法もその一つです。

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

P.エンティティを更新した後、googleId [email protected]でオブジェクトを削除し、データベースに再度書き込みます。それを客観化することができます。

+0

回答ありがとうございます。残念ながら、私のクエリにtransactionless()を追加しても解決できませんでした。 – Jochen

+0

Userオブジェクトを表示します。 – Yevgen

+0

ありがとうYevgen - 私は上記のポストを更新しました。 – Jochen

0

最初にフィールドモデルに@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(); 
関連する問題