Hi all.
I'm new to AppEngine and stuggling with the datastore.
I am used to hibernate, so maybe I expect to much :)

My problem is that when I get a parent from the datastore, the child
field is null.

I have the following 2 to domian objects (from the documentation):
Parent Class Employee:
-----------------------------------
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

     @Persistent
    private ContactInfo contactInfo;

    public Long getId() {
        return id;
    }

     public ContactInfo getContactInfo() {
         return contactInfo;
     }
     public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }
}
-----------------------------------

Child class ContactInfo:
i---------------------------------
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ContactInfo {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String streetAddress;

    public Key getKey() {
         return key;
    }
}
------------------------------------

And a service to save and get:
public class EmployeeService {

        public void saveEmployee(Employee e) {
            PersistenceManager pm = PMF.get().getPersistenceManager();

        Transaction tx = pm.currentTransaction();
            try {
                tx.begin();
                pm.makePersistent(e);
                tx.commit();
            } finally {
                if (tx.isActive()) {
                    tx.rollback();
                }
                pm.close();
            }
        }

        public Employee getEmployee(long id) {
            PersistenceManager pm = PMF.get().getPersistenceManager();
            try {
                return pm.getObjectById(Employee.class, id);
            } finally {
                pm.close();
            }
        }
}
-----------------------------

Now a small test. when I do this in my Servlet:

        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                        throws IOException {
                resp.setContentType("text/plain");

                EmployeeService serv = new EmployeeService();
                Employee e = new Employee();
                ContactInfo ci = new ContactInfo();
                e.setContactInfo(ci);
                serv.saveEmployee(e);
                resp.getWriter().println(e.getContactInfo().getKey() != null ?
"contactInfo now has a key": "contactInfo did not get a key!");

                Employee employeeFromDB = serv.getEmployee(e.getId());

                resp.getWriter().println(employeeFromDB.getContactInfo() != 
null ?
"contactInfo found" : "no contactInfo in employee!");
        }

The output is:
contactInfo now has a key
no contactInfo in employee!


So my conclution is that contactInfo was stored correctly, but when
getting the Employee from the datastore, it does not have the
ContactInfo.

My question is: is this expected behaviour? And if so how do I get the
contactInfo from Employee without knowing the ID of the contactInfo?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to