Embedded classes allow you to model a field value using a class without creating a new datastore entity and forming a relationship.
你可以使用嵌套类作为POJO的属性,这样,嵌套类就不会作为单独实体被保存在datastore中,也不和POJO类形成关联关系。
The embedded class does not need a primary key field because it is not stored as a separate entity.
嵌套类不需要主键,因为它不作为单独实体被保存。
If you have more than one field on the object whose type is an embedded class, you must rename the fields of one so they do not conflict with another.
如果你的POJO类中有两个或更多的属性,其类型属于同一嵌套类,那么你必须修改其中的一个或多个属性。
修改方式见下面的代码段:
@PersistenceCapable(identityType = IdentityType.APPLICATION) public class EmployeeContacts { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) Long id; @Persistent @Embedded private ContactInfo homeContactInfo; @Persistent @Embedded(members = { @Persistent(name = "streetAddress", columns = @Column(name = "workStreetAddress")), @Persistent(name = "city", columns = @Column(name = "workCity")), @Persistent(name = "stateOrProvince", columns = @Column(name = "workStateOrProvince")), @Persistent(name = "zipCode", columns = @Column(name = "workZipCode")), }) private ContactInfo workContactInfo; @PersistenceCapable @EmbeddedOnly public static class ContactInfo { @Persistent private String streetAddress; @Persistent private String city; @Persistent private String stateOrProvince; @Persistent private String zipCode; // ... accessors ... } // ... accessors ... }
将其保存后,查看Datastore Viewer,见附件。