2017-05-24 7 views
0

Google App EngineでObjectifyをData Store用に使用しています。私は6つのプロパティを持つエンティティを持っています。 例えば、Google App Engineデータストアのエンティティを部分的に更新する方法

  • キー(ID)
  • 年齢
  • MOBILE_NUMBER
  • メール

    私は、エンティティの更新エンドポイントを記述する必要があります。私は、EndpointがエンティティをEntire Entity以外の指定されたフィールドで更新できるようにする必要があります。例mobile_numberを更新する場合は、携帯電話番号のみを更新する必要があります。または、私がファーストネームを更新したいのであれば、それだけを更新するべきです。

このため、フィールドに基づいてエンティティを更新する一般的な方法を記述する必要があります。

アドバンスで多くの感謝!

答えて

0

はいこのアップデートは次の方法で修正されました。

入力パラメータをハッシュマップとして取得します。ここでは、更新するユーザーのプロパティを取得できます。ハッシュマップのキーは、エンティティのプロパティ値と同じである必要があります。

public Response update(HashMap<String, String> ProfileEntity) { 
//update logic 
} 

と場合は、次の条件に基づいて値更新:私はこのために、スプリング々BeanUtilsを使用

 ProfileEntity existingEntity = getById(ProfileEntity.get("key")); 

     if(existingEntity == null) 
      System.out.println("Invalid Profile Key"); 

     if(ProfileEntity.containsKey("firstName")) 
      existingEntity.setFirstName(ProfileEntity.get("firstName")); 

     if(ProfileEntity.containsKey("lastName")) 
      existingEntity.setLastName(ProfileEntity.get("lastName")); 

     if(ProfileEntity.containsKey("age")) 
      existingEntity.setAge(ProfileEntity.get("age"))); 

     if(ProfileEntity.containsKey("mobile_Number")) 
      existingEntity.setMobileNumber(ProfileEntity.get("mobile_Number")); 

     super.save(existingEntity); 
0

エンティティをトランザクション内にロードして保存します。

トランザクションは、すべての作業ユニットが効率的に連続して実行されることを保証します。 は実際にはで実行されますが、すべての作業単位がシリ​​アル化されているかのようにデータは一貫しています。

+0

thing.setWhatever()については、更新するフィールドを知っておく必要がありますが、setfirstName()またはsetMobileNumber()を設定することができます。 – siva

0

を。

public static String[] getNullPropertyNames(Object source) { 
     final BeanWrapper src = new BeanWrapperImpl(source); 
     java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors(); 

     Set<String> emptyNames = new HashSet<String>(); 
     for (java.beans.PropertyDescriptor pd : pds) { 
      Object srcValue = src.getPropertyValue(pd.getName()); 
      if (srcValue == null) emptyNames.add(pd.getName()); 
     } 
     String[] result = new String[emptyNames.size()]; 
     return emptyNames.toArray(result); 
    } 

    public static void copyProperties(Object src, Object target) { 
     BeanUtils.copyProperties(src, target, getNullPropertyNames(src)); 
    } 

次に、この

public Publisher update(@Named("id") Long id, Publisher publisher) throws Exception { 
     checkExists(id); 
     Publisher destination = get(id); 
     copyProperties(publisher, destination); 
     return insert(destination); 
    } 

NOTEのようなあなたのアップデートを使用します。このメソッドはnullを、リストのプロパティをオーバーライドしません。

関連する問題