对象化并通过电子邮件发送为 @Id

发布于 2024-12-07 02:56:49 字数 1608 浏览 2 评论 0原文

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

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

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

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

提前谢谢您!

I am not sure if I use the @Id in objectify the right way.
Right now I am using the eMail-Address as @Id field. The email field will be set on the server-side only (OAuthService.getCurrentUser.getEmail)

First question: Is this a good idea?

If I create for example an Item-class which has RegistrationTO as it's parent does it make sense to use the email-address as the @Id field in my Item-class or should Item-class have it's own, auto-generated, id and Key parent to specify the relation?

Objectify-Tutorial recommends to avoid @Parent - so, here I think it's not necessary either.
I am right?

Here my RegistrationTO:

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

Sample for Item class:

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

@Id
Long id

//or
//@Id
//String email

Key<RegistrationTO> parent;

String itemno;
    }

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2024-12-14 02:56:49

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

现在,关于 Item 类的 @Id,如果电子邮件唯一标识每个对象,则无需创建新的自动生成的 Long 作为 @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

希望有帮助!

Regarding your question if the use of e-mail as @Id is correct or not, since the email will uniquely identify each object of the class, then you are fine!

Now, regarding the @Id of your Item class, if the email uniquely identifies each object, then there is no need to create a new auto-generated Long as @Id. In general, the criterion for the selection of the @Id is to uniquely identify all the objects of the class.

For the relationship between RegistrationTO and Item classes, use the @Parent annotation only if you need these entities to be the same entity group. The code for this:

@Parent
Key<RegistrationTO> parent;

Otherwise, use a "plain" relationship (as you have it in your example) that allows RegistrationTO and Item entities to be stored in different entity groups in the GAE datastore. For more information about entity groups, take a look at:
http://code.google.com/appengine/docs/java/datastore/entities.html#Entity_Groups_and_Ancestor_Paths

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文