use of org.springframework.data.mongodb.InvalidMongoDbApiUsageException in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method testUsingInQueryWithList.
@Test
public void testUsingInQueryWithList() throws Exception {
template.remove(new Query(), PersonWithIdPropertyOfTypeObjectId.class);
PersonWithIdPropertyOfTypeObjectId p1 = new PersonWithIdPropertyOfTypeObjectId();
p1.setFirstName("Sven");
p1.setAge(11);
template.insert(p1);
PersonWithIdPropertyOfTypeObjectId p2 = new PersonWithIdPropertyOfTypeObjectId();
p2.setFirstName("Mary");
p2.setAge(21);
template.insert(p2);
PersonWithIdPropertyOfTypeObjectId p3 = new PersonWithIdPropertyOfTypeObjectId();
p3.setFirstName("Ann");
p3.setAge(31);
template.insert(p3);
PersonWithIdPropertyOfTypeObjectId p4 = new PersonWithIdPropertyOfTypeObjectId();
p4.setFirstName("John");
p4.setAge(41);
template.insert(p4);
List<Integer> l1 = new ArrayList<Integer>();
l1.add(11);
l1.add(21);
l1.add(41);
Query q1 = new Query(Criteria.where("age").in(l1));
List<PersonWithIdPropertyOfTypeObjectId> results1 = template.find(q1, PersonWithIdPropertyOfTypeObjectId.class);
Query q2 = new Query(Criteria.where("age").in(l1.toArray()));
List<PersonWithIdPropertyOfTypeObjectId> results2 = template.find(q2, PersonWithIdPropertyOfTypeObjectId.class);
assertThat(results1.size(), is(3));
assertThat(results2.size(), is(3));
try {
List<Integer> l2 = new ArrayList<Integer>();
l2.add(31);
Query q3 = new Query(Criteria.where("age").in(l1, l2));
template.find(q3, PersonWithIdPropertyOfTypeObjectId.class);
fail("Should have trown an InvalidDocumentStoreApiUsageException");
} catch (InvalidMongoDbApiUsageException e) {
}
}
Aggregations