2017-04-11 17 views
0

おはようございます、私はasinconaクラスからバックエンドを呼び出したアンドロイドアプリケーションでオブジェクトエンジンを使用してappengineバックエンドを更新しています。 いくつかのチュートリアルを読んだ後で、エンティティを取得、更新、挿入、削除することができましたが、このエンティティには別のタイプのエンティティのarraylistが含まれています。エンティティのリストへのアクセス、私はさらに挿入を行うことができましたが、更新と削除を行う時には動作させることができません。objectity他のエンティティ内のエンティティを処理する方法

例エンティティユーザは、写真のエンティティのリストを持っています

@Entity 
public class UserEntity { 
@Id 
String id; 
private String mail; 
private String name; 
private ArrayList<PhotoEntity> listPhotos = new ArrayList<>(); 

//constructor 
public UserEntity(){} 

getters & setters.... 
} 

フォトエンティティ:UserEntity

@Entity 
public class PhotoEntity { 
@Id 
Long id; 
private String author; 
private int height; 
private int widht; 
private Blob image; 

//constructor 
public PotoEntity() {} 

getters & setters..... 
} 

エンドポイントの挿入方法は働く:

@ApiMethod(
     name = "insertUserPhoto", 
     path = "userEntityPhoto", 
     httpMethod = ApiMethod.HttpMethod.POST) 
public UserEntity insertUserPhoto(@Named("idUser")String idUser, UserPhotoEntity userPhoto) throws NotFoundException { 
    UserEntity userEntity = null; 
    userEntity = ofy().load().type(UserEntity.class).id(idUser).now(); 
    if (userEntity == null) { 
     throw new NotFoundException("Could not find UserEntity with ID: " + idUser); 
    } 
    userEntity.getListPhotos().add(userPhoto); 
    ofy().save().entity(userEntity).now(); 
    logger.info("Created UserPhotoEntity with ID: " + userPhoto.getId()); 

    return ofy().load().entity(userEntity).now(); 
} 
@ApiMethod(
     name = "updateUserPhoto", 
     path = "userEntityPhoto/{id}", 
     httpMethod = ApiMethod.HttpMethod.PUT) 
public UserEntity updateUserPhoto(@Named("idUser") String idUser, 
              @Named("idPhoto") String idPhoto) throws Exception { 

    UserEntity userEntity = null; 
    UserPhotoEntity userPhotoTemp = null; 

    userEntity = ofy().load().type(UserEntity.class).id(idUser).now(); 
    if (usuarioEntity == null) { 
     throw new NotFoundException("Could not find UserEntity with ID: " + idUser); 
    } 

    for(int i = 0; i < userEntity.getListPhotos().size(); i++){ 
     if(userEntity.getListPhotos().get(i).getId().equalsIgnoreCase(idPhoto)){ 
      userPhotoTemp = userEntity.getListPhotos().get(i); 
      userPhotoTemp.setAuthor("code changed"); 

      userEntity.getListPhotos().get(i).remove(); 
      userEntity.getListPhotos().add(userPhotoTemp); 

      ofy().save().entity(userEntity).now(); 
      break; 
     } 
    } 

    logger.info("Updated UserPhotoEntity: " + userEntity); 
    return ofy().load().entity(userEntity).now(); 
} 

deleteメソッドは動作しません:更新方法が機能しない210

@ApiMethod(
     name = "removeUserPhoto", 
     path = "userEntityPhoto/{id}", 
     httpMethod = ApiMethod.HttpMethod.DELETE) 
public void removeUserPhoto(@Named("idUser") String idUser, 
           @Named("idPhoto") String idPhoto) throws NotFoundException { 

    UserEntity userEntity = null; 

    userEntity = ofy().load().type(UserEntity.class).id(idUser).now(); 
    if (userEntity == null) { 
     throw new NotFoundException("Could not find UserEntity with ID: " + idUser); 
    } 

    for(UserPhotoEntity upe : userEntity.getListPhotos()){ 
     if(upe.getId().equalsIgnoreCase(idPhoto)){ 
      ofy().delete().entities(upe); 
      break; 
     } 
    } 

    logger.info("Deleted UserEntity with ID: " + idPhoto); 
} 

誰かがエンティティのリストがあるとき、私はエンドポイントのメソッドを管理すべきか、私を助けることができますか?

ありがとうございました。作成し、埋め込まれたエンティティう

+0

あなたはデータを埋め込むと 'Key'または' Ref'でデータを参照との違いをよく読んでする必要があります。埋め込みデータを参照として処理しようとしているようです。 https://github.com/objectify/objectify/wiki/Entities – stickfigure

答えて

0

私はあなたが参考に変化することを示唆している:

@Entity 
public class UserEntity { 
@Id 
String id; 
private String mail; 
private String name; 
private List<Ref<PhotoEntity>> photoEntity= new ArrayList<Ref<PhotoEntity>>(); 
+0

エラー:タスク ':バックエンド:appengineEndpointsGetClientLibs'の実行に失敗しました。 >エンドポイント・コマンドget-client-libを実行中にエラーが発生しました。パラメーター化されたタイプcom.googlecode.objectify.Ref はサポートされていません。 – user1802692

+0

私はgettersとsettersにこのアノテーションを入れて、@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)をコンパイルしようとしていますが、今度はリストを設定するメソッドが表示されない私のアプリケーションコードには – user1802692

+0

という質問があります。エンティティ)は動作し、更新と削除は機能しませんか? – user1802692

関連する問題