google-code-export / morphia

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

NPE when using "in" query with @Id field #317

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Versions: 
Morphia 1.00-SNAPSHOT / r1762
MongoDB driver 2.6.5
MongoDB 1.9.1

I have a class

@Entity("docs")
public class Doc
{
   @Id public long id;

}

Now, I want to get a subset of all docs, defined by a list of ids.
But, I also want to apply other search criteria. Thus, I want to build
a query. When I call the "in" operator on the _id field with a list of
keys, I get an NPE.

Sample code:

      Doc doc = new Doc();
      doc.id = 1L;
      ds.save( doc );

      // this works
      List< Doc > docs1 = ds.find( Doc.class ).field( "_id" ).equal( 1L ).asList();

      List< Long > idList = new ArrayList< Long >();
      idList.add( 1L );
      // this causes an NPE
      List< Doc > docs2 = ds.find( Doc.class ).field( "_id" ).in( idList ).asList();

Stack trace:

Exception in thread "main" java.lang.NullPointerException
        at com.google.code.morphia.mapping.Mapper.toMongoObject(Mapper.java:352)
        at com.google.code.morphia.query.FieldCriteria.<init>(FieldCriteria.java:47)
        at com.google.code.morphia.query.FieldEndImpl.addCrit(FieldEndImpl.java:35)
        at com.google.code.morphia.query.FieldEndImpl.hasAnyOf(FieldEndImpl.java:120)
        at com.google.code.morphia.query.FieldEndImpl.in(FieldEndImpl.java:124)
        at com.google.code.morphia.query.FieldEndImpl.in(FieldEndImpl.java:11)
        at com.test.MorphiaTestCase.main(MorphiaTestCase.java:29) 

Original issue reported on code.google.com by sdeit...@googlemail.com on 29 Aug 2011 at 12:54

GoogleCodeExporter commented 9 years ago
This bug seems to affect not only queries of @Id fields, but any "in" query on 
non-reference, single-value fields. 

My analysis:
Mapper#toMongoObject tries to find out whether the field that is used in the 
query is a reference field, or a multi-value field with reference-type values. 
Checking for the latter is done when the query value is an Iterable, but causes 
an NPE when the field is a single-value field and thus has no sub-type to check.

If my analysis is correct, you can apply the attached patch. It works for me, 
but I'm not sure I understood all its consequences.

Original comment by sdeit...@googlemail.com on 20 Sep 2011 at 9:29

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by scotthernandez on 20 Sep 2011 at 2:25