How to annotate a collection of embedded objects in Google App Engine (Java)?

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:

// 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();
}

The test code always displays "null". Any suggestions?

contactDetails @Embedded, DataNucleus . defaultFetchGroup = "true" @Persistent contactDetails ( , ), . - , , ContactInfo ( , , , ).

? , , , scneario, , .

+5
2

, , 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...

}
+2

All Articles