Can I store a collection of built-in classes in Google App Engine (Java)? If so, how do I annotate it?
This is my class:
@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;
}
}
}
Here is the test code:
Employee emp = new Employee();
emp.addContactInfo("123 Main Street", "Some City");
emp.addContactInfo("50 Blah Street", "Testville");
PersistenceManager pm = PMF.get().getPersistenceManager();
try
{
pm.makePersistent(emp);
}
finally
{
pm.close();
}
pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(Employee.class);
try
{
List<Employee> employees = (List<Employee>) query.execute();
for(Employee fetchedEmployee : employees)
{
resp.getWriter().println(fetchedEmployee.getContactDetails());
}
}
finally
{
query.closeAll();
pm.close();
}
The test code always displays "null". Any suggestions?
contactDetails @Embedded, DataNucleus . defaultFetchGroup = "true" @Persistent contactDetails ( , ), . - , , ContactInfo ( , , , ).
? , , , scneario, , .