Cam*_*ron 5 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试代码:
// 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();
}
Run Code Online (Sandbox Code Playgroud)
测试代码总是输出'null'.有什么建议?
我已尝试将contactDetails列表注释为@Embedded,但DataNucleus增强器引发了异常.我还尝试将defaultFetchGroup ="true"添加到contactDetails列表上的@Persistent注释中(如果它是获取数据的问题),但输出仍为null.我已经在控制台中检查了员工对象,并且没有任何证据表明在对象上存储了任何联系人详细信息字段,数据存储区中也没有任何独立的ContactInfo对象(不是我期望的那样,给定它们)是嵌入的对象).
这甚至可能吗?我知道嵌入式类的字段存储为实体的属性,因此我可以看到集合scneario中可能出现字段名称冲突,但App Engine文档没有明确说明无法完成.
使用 JDO,您可以按照 DataNucleus 文档进行操作 http://www.datanucleus.org/products/accessplatform_2_2/jdo/orm/embedded.html#Collection 该示例显示 XML,但注释名称大致相同。
我不能说 GAE/J 的插件是否支持这个,因为这是 Google 的责任。也许尝试一下?
| 归档时间: |
|
| 查看次数: |
1940 次 |
| 最近记录: |