【问题标题】:unique constraints-Google app engine [duplicate]独特的约束-Google 应用引擎 [重复]
【发布时间】:2011-02-28 02:29:49
【问题描述】:

可能重复:
Unique Constraint At Data Level in GAE

Google App 引擎数据存储是否支持唯一约束?

【问题讨论】:

  • 别抱怨了......谁在乎,人们已经回答了它

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


【解决方案1】:

并非如此,但键可以保证是唯一的,您可以将实体的键设置为任意字符串。当您将实体保存到数据存储时,您可以利用这一点创建唯一键。例如,

public class Entity implements Serializable {

    @Id String id;

    String unique_together_1;
    String unique_together_2;

    public Entity (String unique_together_1, String unique_together_2) {

    this.id = unique_together_1 + "-" + unique_together_2;
    this.unique_together_1 = unique_together_1;
    this.unique_together_2 = unique_together_2;
    }

如果您的实体的唯一字段稍后发生更改,或者您需要针对单个实体类型的多个唯一约束,这显然不起作用。

【讨论】:

  • 这是个好主意。如果你这样做,当你 put() 一个共享 unique_together1unique_together_2 的对象时,它会简单地覆盖数据存储中的任何现有对象。您可能希望使用事务来检查其密钥是否已在数据存储中,如果没有,则添加它。
猜你喜欢
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多