google-app-engine - 如何在 Google App Engine (Java) 中注释嵌入式对象的集合?

标签 google-app-engine jdo

是否可以在 Google App Engine (Java) 中存储一组嵌入式类?如果是这样,我将如何注释它?

这是我的课:

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

    @Persistent(embeddedElement = "true")
    private List<ContactInfo> contactDetails = new ArrayList<ContactInfo>();

    public void addContactInfo(String streetAddress, String city)
    {
        this.contactDetails.add(new ContactInfo(streetAddress, city));
    }

    public List<ContactInfo> getContactDetails()
    {
        return this.contactDetails;
    }

    @PersistenceCapable
    @EmbeddedOnly
    public static class ContactInfo
    {
        @Persistent
        private String streetAddress;

        @Persistent
        private String city;

        public ContactInfo(String streetAddress, String city)
        {
            this.streetAddress = streetAddress;
            this.city = city;
        }

        public String getStreetAddress()
        {
            return this.streetAddress;
        }

        public String getCity()
        {
            return this.city;
        }
    }
}

测试代码如下:

// Create the employee object

Employee emp = new Employee();

// Add some contact details

emp.addContactInfo("123 Main Street", "Some City");
emp.addContactInfo("50 Blah Street", "Testville");

// Store the employee

PersistenceManager pm = PMF.get().getPersistenceManager();

try
{
    pm.makePersistent(emp);
} 
finally
{
    pm.close();
}

// Query the datastore for all employee objects

pm = PMF.get().getPersistenceManager();

Query query = pm.newQuery(Employee.class);

try
{
    List<Employee> employees = (List<Employee>) query.execute();

    // Iterate the employees

    for(Employee fetchedEmployee : employees)
    {
        // Output their contact details

        resp.getWriter().println(fetchedEmployee.getContactDetails());
    }
}
finally
{
    query.closeAll();
    pm.close();
}

测试代码总是输出'null'。有什么建议吗?

我已经尝试将 contactDetails 列表注释为 @Embedded,但 DataNucleus 增强器引发了异常。我还尝试将 defaultFetchGroup = "true"添加到 contactDetails 列表上的 @Persistent 注释(以防获取数据时出现问题),但输出仍然为空。我检查了控制台中的员工对象,没有证据表明该对象上存储了任何联系人详细信息字段,数据存储区中也没有任何独立的 ContactInfo 对象(不是我期望的,因为它们是嵌入式对象)。

这甚至可以做到吗?我知道嵌入式类的字段存储为实体的属性,因此我可以看到集合场景中可能会出现字段名称冲突,但是 App Engine docs不要明确地说它不能完成。

最佳答案

我不知道您是否仍然需要答案,但是将 List 放入 defaultFetchGroup 为我解决了这个问题:

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

    @Persistent(embeddedElement = "true", defaultFetchGroup = "true")
    private List<ContactInfo> contactDetails = new ArrayList<ContactInfo>();

    // Snip...

}

关于google-app-engine - 如何在 Google App Engine (Java) 中注释嵌入式对象的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3775228/

相关文章:

python - 如何获取我自己帐户的访问 token 以在我的服务器中存储/使用?

python - 从数据存储查询返回列表时, fetch() 是否比 list(Model.all().run()) 更好?

java - GAE : File uploading failing on live

java - 如何使用 JSON 数据发送 POST 方法 (java)?

google-app-engine - 使对象持久化时出错

java - 具有相同类型子项的 App Engine 类

java - 更新 GAE 中的对象

python - 在 Google App Engine 中定义 Python 函数

java - Google App Engine 数据存储事务的两种不同方法——使用哪一种?

java - 无法在没有 JDO 注释的情况下使用 DataNucleus 增强器