【问题标题】:How to instert child entities in JDO (Google App Engine)?如何在 JDO(Google App Engine)中插入子实体?
【发布时间】:2010-06-10 21:11:16
【问题描述】:

如何在下面的示例中向子实体添加记录?例如 我有一个员工记录,名字是“Sam”。如何为 sam 添加 2 个街道地址?

猜猜我有一个

父实体是员工

import java.util.List;

// ...
@Persistent(mappedBy = "employee")
private List<ContactInfo> contactInfoSets;

子键是地址

import com.google.appengine.api.datastore.Key;
// ... imports ...

@PersistenceCapable
public class ContactInfo {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String streetAddress;

    // ...
}

【问题讨论】:

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


    【解决方案1】:

    它只是工作:

    Employee sam = new Employee("Sam");
    List<Address> addresses = new ArrayList<Address>();
    addresses.add(new Address("Foo St. 1"));
    addresses.add(new Address("Bar Bvd. 3"));
    sam.setAddresses(addresses);
    persistenceManager.makePersistent(sam);
    

    员工身份:

    @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
    public class Employee {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;
        private List<Address> addresses;
        ...
    }
    

    地址是:

    @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
    public class Address {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;
        ...
    }
    

    使用@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true") 作为类级别注释。通常您不需要注释除键之外的任何其他字段,因此List 上的@Persistent(mappedBy = "employee") 是不必要的。

    顺便说一句。我建议使用参数化集合。

    【讨论】:

    • 嗨 Hleinone。感谢您的回答。您建议使用参数化集合,但它是什么?我对应用引擎很陌生,但我有数据库知识。还有为什么 @Persistent(mappedBy = "employee") 部分是不必要的?
    • 使用参数化集合意味着您使用 List 而不仅仅是 List。关于另一个问题。在@PersistenceCapable 类中,所有不是staticfinaltransient 并且属于持久类型的字段默认情况下都会持久化。 mappedBy 用于双向关系,这种情况下可能不需要。
    【解决方案2】:

    可以通过以下方式插入和检索子条目:

    父类人

    @PersistenceCapable(identityType=IdentityType.APPLICATION,detachable = "true")
    public class Person {
    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    Long id ;
    @Persistent
    String strName;
    @Persistent
    String phnNumber;
    @Persistent
    String strEmail;
    @Nullable
    @Persistent(defaultFetchGroup="true")
    @Element(dependent="true")
    //When adding Person Contacts would be added as it is dependent. Also while retrieving
    // add defaultFetchGroup = true to retrieve child elements along with parent object.
    List<Contacts> lstContacts;
    
    // getter and setter methods
    

    }

    从属子类:联系人

    @PersistenceCapable(identityType=IdentityType.APPLICATION,detachable = "true")
    public class Contacts 
    {
    @PrimaryKey  
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    Key id;
    @Persistent
    String email;
    @Persistent
    String phNumber;
       //getter and setter methods
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 2014-08-23
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      相关资源
      最近更新 更多