【问题标题】:Does objectify have a get_or_insert equivalent是否 objectify 有一个 get_or_insert 等价物
【发布时间】:2016-04-18 14:39:12
【问题描述】:

数据存储的 python 版本有一个 get_or_insert() 方法。 Objectify 是否提供类似的功能?

【问题讨论】:

    标签: google-app-engine google-cloud-datastore objectify


    【解决方案1】:

    不幸的是,Objectify 目前没有这样的功能。但是,您可以自己实现类似的东西。

    public Something getOrInsert(Something s) {    
        return ofy().transact(new Work<Something>() {
            public Something run() {
                Something something = ofy().load().type(Something.class).id(s.getId()).now();
                if(something != null) {
                    return something;
                }
    
                ofy().save().entity(s);
    
                return s;
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 2011-01-16
      • 2020-09-20
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多