[appengine-java] Re: JDO queries on owned/unowned one-to-many relationships

2009-09-04 Thread leszek

You can run queries on keys:

http://code.google.com/appengine/docs/java/datastore/queriesandindexes.html#Queries_on_Keys

The first part of child key (ContactInfo.class) contains parent
(Employee) key.

 Employee e;
 

 query = pm.newQuery(ContactInfo.class);
 query.setFilter(key == keyParam);
 query.declareParameters(com.google.appengine.api.datastore.Key
keyParam);
 Key key = KeyFactory.createKey(Employee.class.getSimpleName(),e.getId
());
 list = (ListContactInfo) query.execute(key);

But it returns empty list because the second part of child key
contains child key itself, so it is never equal.

Maybe something like that will work (assuming that your goal is to
find all child belonging to single parent).

 query = pm.newQuery(ContactInfo.class);
 query.setFilter(key  keyParam and key  keyParam1);
 query.declareParameters(com.google.appengine.api.datastore.Key
keyParam);
 query.declareParameters(com.google.appengine.api.datastore.Key
keyParam1);
 Key key = KeyFactory.createKey(Employee.class.getSimpleName(),e.getId
());
 Key key1 = KeyFactory.createKey(Employee.class.getSimpleName(),e.getId
() + 1);
 list = (ListContactInfo) query.execute(key,key1);


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[appengine-java] Re: JDO queries on owned/unowned one-to-many relationships

2009-09-04 Thread leszek


 query.setFilter(key  keyParam and key  keyParam1);
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[appengine-java] Re: JDO queries on owned/unowned one-to-many relationships

2009-09-04 Thread Jason (Google)
Owned relationships uses entity groups behind-the-scenes:

http://code.google.com/appengine/docs/python/datastore/keysandentitygroups.html#Entity_Groups_Ancestors_and_Paths

An entity group is basically a hierarchy of entities, with one entity as the
root and all other entities below this root. Entity groups can be
arbitrarily deep, so you can have entities with a parent, grandparent,
great-grandparent, and so-on. Owned relationships are maintained by
embedding the parent's key in the child's key -- this is why you can't
change the relationship between parent and child after storing the entities.


Because parent keys are embedded in the child entities, it's possible to
execute an ancestor query where you retrieve all entities above a given
entity in the hierarchy. This is pretty straightforward using the low-level
datastore API, but you can do this in JDO as well -- see the last post in
this thread:

http://groups.google.com/group/google-appengine-java/browse_thread/thread/97ba3209ec6a65de

- Jason

On Thu, Sep 3, 2009 at 7:40 PM, Nicholas Albion nalb...@gmail.com wrote:


 Could somebody please provide an example of how to query for the one-
 to-many relationship examples in the documentation?

 What I want to do is something like the following:

 String employeeId = 1234;
 query = pm.newQuery(ContactInfo.class);
 query.setFilter(employee == employeeParam);
 query.declareParameters(String employeeParam);
 ListContactInfo employeeContacts = (ListContactInfo) query.execute
 ( employeeId );

 Should this work?  Or do I need to use an unowned one-to-many,
 replacing private Employee employee; with private Key employeeId;
 and then something like:

 Key employeeId = KeyFactory.createKey(Employee.class.getSimpleName(),
 1234);
 query = pm.newQuery(ContactInfo.class);
 query.setFilter(employeeId == employeeParam);
 query.declareParameters(Key employeeParam);
 ListContactInfo employeeContacts = (ListContactInfo) query.execute
 ( employeeId );

 (I'm a bit confused by the difference between owned and unowned
 relationships)
 


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---