如何在 JDO (Google App Engine) 中插入子实体?

发布于 2024-09-05 08:33:43 字数 558 浏览 2 评论 0原文

如何向下面示例中的子实体添加记录?例如 我有一份员工记录,名字是“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;

    // ...
}

How do i add a record to a child entity in the example below ? For example
i have a Employee Record which is name is "Sam". how do i add 2 street adress for sam ?

Guess i have a

The Parent entity is Employee

import java.util.List;

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

The Child key is Adress

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

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

    @Persistent
    private String streetAddress;

    // ...
}

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

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

发布评论

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

评论(2

甜`诱少女 2024-09-12 08:33:43

它只是有效:

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") 是不必要的。

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

It just works:

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

Employee being:

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

Address being:

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

Use @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true") as the class level annotation. Usually you don't need to annotate any other fields but the key, so the @Persistent(mappedBy = "employee") on the List is unnecessary.

Btw. I suggest using parametrized collections.

无畏 2024-09-12 08:33:43

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

父类 Person

@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

}

依赖子类 : Contacts

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

Inserting and retrieving Child Entries can be done in the following way:

Parent class Person

@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

}

Dependent Child Class : Contacts

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