google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Query referenced ID #408

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I don't know if this is an issue or pilot error.  Consider the canonical blog 
example where Blog Posts have an @Reference to an Author.

If I want to query blog posts written by that author (querying by the author 
ID), I have to turn off query validation or it complains that I am trying to go 
past the dot on a reference.

When I disable validation, the query works as expected.  FWIW, I am using the 
Grails plugin and my line looks like:

field('person.$id', false).equal(personId)

If I leave the false out, the query doesn't validate.

Is there a better way to do this?

Original issue reported on code.google.com by andy.da...@koanhealth.com on 22 May 2012 at 3:41

GoogleCodeExporter commented 9 years ago
Yes, you should use the instance of the person and not use the internal parts 
of the DBRef like $id.

I don't know if the grails plugin is doing this or if you did it manually.

Original comment by scotthernandez on 22 May 2012 at 3:44

GoogleCodeExporter commented 9 years ago
Thanks for the speedy response.  So it should be:

def loadedPerson = Person.get(personId);
field('person').equal(loadedPerson)

Is this correct?  And are examples of this documented?  I hesitated to even ask 
the question because of the ubiquity of the blog example; but I cannot seem to 
find this sort of query demonstrated anywhere.

Original comment by andy.da...@koanhealth.com on 22 May 2012 at 3:49

GoogleCodeExporter commented 9 years ago
Following up for posterity.

Yes, the correct way to do this is:

field('person').equal(person);

person can either be a loaded object or a dummy that only has the id filled in.

Original comment by andy.da...@koanhealth.com on 22 May 2012 at 7:13

GoogleCodeExporter commented 9 years ago
And to directly answer you question, this was something that I was doing and 
had nothing to do with the Grails plugin.  

It just wasn't clear how I went from a person ID to a list of items that 
referenced the person ID.

Original comment by andy.da...@koanhealth.com on 22 May 2012 at 7:34