简体   繁体   English

如何部分更新Google App Engine数据存储中的实体

[英]How to Update an Entity in a Google app engine data store partially

I am using Objectify in Google App Engine for Data Store. 我在Google App Engine for Data Store中使用Objectify。 I am having an Entity with 6 Properties. 我有一个具有6个属性的实体。 For Example, 例如,

  • Key (id) 密钥(ID)
  • FirstName 名字
  • LastName
  • Age 年龄
  • mobile_Number 手机号码
  • Email 电子邮件

    I need to write an Update Endpoint for the Entity. 我需要为实体编写一个更新端点。 I want that update Endpoint should be able to update the Entity with the specified fields Not the Entire Entity. 我希望该更新端点应该能够使用指定的字段而非整个实体来更新实体。 For Example If I want to update the mobile_number , it should update the mobile number alone. 例如,如果我想更新mobile_number ,它应该单独更新手机号码。 or If i want to update the firstName it should update that only. 或者,如果我想更新firstName,它应该只更新它。

For that I need to write a common method to update the Entity based on the fields. 为此,我需要编写一个通用方法来基于字段更新实体。

Many thanks in Advance! 提前谢谢了!

You load and save the entity in a transaction. 您在事务中加载并保存实体。

ofy().transact(() -> {
    Thing thing = ofy().load().key(key).now();
    thing.setWhatever();
    ofy().save().entity(thing);
});

Transactions guarantee that all units of work are effectively executed in serial. 事务保证所有工作单元都可以有效地串行执行。 Not actually executed in serial, but the data will be consistent as if all units of work were serialized. 实际上并没有串行执行,但是数据将保持一致,就好像所有工作单元都已串行化一样。

Yes i fixed this update in the following way. 是的,我以以下方式修复了此更新。

Got the input parameter as Hashmap where i can get the user's properties that to be updated. 得到的输入参数为Hashmap,在这里我可以获取要更新的用户属性。 The hashmap's key should be same as Entity's property value. 哈希图的键应与Entity的属性值相同。

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

and updated the value based on the following if condition: 并根据以下条件更新值:

        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);

I used spring BeanUtils for this. 我为此使用了Spring BeanUtils。

 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));
    }

Then use your update like this 然后像这样使用您的更新

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

NOTE: This method will not overide null and list properties. 注意:此方法不会覆盖null和list属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM