【问题标题】:Objectify and email as @Id对象化并以@Id 发送电子邮件
【发布时间】:2011-11-27 18:47:24
【问题描述】:

我不确定我是否以正确的方式在对象化中使用 @Id。 现在我使用电子邮件地址作为@Id 字段。电子邮件字段将仅在服务器端设置 (OAuthService.getCurrentUser.getEmail)

第一个问题:这是个好主意吗?

例如,如果我创建一个 Item-class,它具有 RegistrationTO 作为它的父级,那么在我的 Item-class 中使用电子邮件地址作为 @Id 字段是否有意义,或者 Item-class 是否应该拥有它自己的自动生成, id 和 Key parent 指定关系?

Objectify-Tutorial 建议避免使用@Parent - 所以,我认为也没有必要。 我说的对吗?

这里是我的注册地址:

public class RegistrationTO implements Serializable {
private static final long   serialVersionUID    = 1L;

@NotNull
@Size(min = 5, max = 20)
private String              firstname;

@NotNull
@Size(min = 5, max = 20)
private String              name;

@NotNull
@Size(min = 5, max = 20)
private String              country;

@Id
@NotNull
@Size(min = 5, max = 20)
@Pattern(regexp = "\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\b")
private String              email;

public RegistrationTO() {

}

public RegistrationTO(final String firstname, final String name, final String company) {
    this.firstname = firstname;
    this.name = name;
    this.country = country;
    email = "will be set on server (Oauth)";
}

public String getFirstname() {
    return firstname;
}

public String getName() {
    return name;
}

public String getCountry() {
    return country;
}

public String getEmail() {
    return email;
}

public void setEmail(final String email) {
    this.email = email;
}
  }

项目类示例:

public class Item implements Serializable {
private static final long   serialVersionUID    = 1L;

@Id
Long id

//or
//@Id
//String email

Key<RegistrationTO> parent;

String itemno;
    }

提前谢谢你!

【问题讨论】:

    标签: google-app-engine gwt objectify


    【解决方案1】:

    关于您的问题,如果使用电子邮件作为@Id 是否正确,因为电子邮件将唯一标识该类的每个对象,那么您就可以了!

    现在,关于您的 Item 类的 @Id,如果电子邮件唯一标识每个对象,则无需创建新的自动生成 Long as @Id。一般来说,@Id 的选择标准是唯一标识该类的所有对象。

    对于RegistrationTO和Item类之间的关系,只有在需要这些实体是同一个实体组的情况下才使用@Parent注解。代码:

    @Parent
    Key<RegistrationTO> parent;
    

    否则,请使用“普通”关系(如您在示例中所使用的),该关系允许 RegistrationTO 和 Item 实体存储在 GAE 数据存储中的不同实体组中。有关实体组的更多信息,请查看: http://code.google.com/appengine/docs/java/datastore/entities.html#Entity_Groups_and_Ancestor_Paths

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多