【发布时间】:2011-02-28 02:29:49
【问题描述】:
【问题讨论】:
-
别抱怨了......谁在乎,人们已经回答了它
标签: google-app-engine jpa google-cloud-datastore
【问题讨论】:
标签: google-app-engine jpa google-cloud-datastore
并非如此,但键可以保证是唯一的,您可以将实体的键设置为任意字符串。当您将实体保存到数据存储时,您可以利用这一点创建唯一键。例如,
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;
}
如果您的实体的唯一字段稍后发生更改,或者您需要针对单个实体类型的多个唯一约束,这显然不起作用。
【讨论】:
unique_together1 和 unique_together_2 的对象时,它会简单地覆盖数据存储中的任何现有对象。您可能希望使用事务来检查其密钥是否已在数据存储中,如果没有,则添加它。