コード例 #1
0
  @Test
  public void testUsingAnInQueryWithPrimitiveIntId() throws Exception {

    template.remove(new Query(), PersonWithIdPropertyOfPrimitiveInt.class);

    PersonWithIdPropertyOfPrimitiveInt p1 = new PersonWithIdPropertyOfPrimitiveInt();
    p1.setFirstName("Sven");
    p1.setAge(11);
    p1.setId(1001);
    template.insert(p1);
    PersonWithIdPropertyOfPrimitiveInt p2 = new PersonWithIdPropertyOfPrimitiveInt();
    p2.setFirstName("Mary");
    p2.setAge(21);
    p2.setId(1002);
    template.insert(p2);
    PersonWithIdPropertyOfPrimitiveInt p3 = new PersonWithIdPropertyOfPrimitiveInt();
    p3.setFirstName("Ann");
    p3.setAge(31);
    p3.setId(1003);
    template.insert(p3);
    PersonWithIdPropertyOfPrimitiveInt p4 = new PersonWithIdPropertyOfPrimitiveInt();
    p4.setFirstName("John");
    p4.setAge(41);
    p4.setId(1004);
    template.insert(p4);

    Query q1 = new Query(Criteria.where("age").in(11, 21, 41));
    List<PersonWithIdPropertyOfPrimitiveInt> results1 =
        template.find(q1, PersonWithIdPropertyOfPrimitiveInt.class);
    Query q2 = new Query(Criteria.where("firstName").in("Ann", "Mary"));
    List<PersonWithIdPropertyOfPrimitiveInt> results2 =
        template.find(q2, PersonWithIdPropertyOfPrimitiveInt.class);
    Query q3 = new Query(Criteria.where("id").in(1001, 1003));
    List<PersonWithIdPropertyOfPrimitiveInt> results3 =
        template.find(q3, PersonWithIdPropertyOfPrimitiveInt.class);
    assertThat(results1.size(), is(3));
    assertThat(results2.size(), is(2));
    assertThat(results3.size(), is(2));
  }
コード例 #2
0
  private void testProperHandlingOfDifferentIdTypes(MongoTemplate mongoTemplate) throws Exception {

    // String id - generated
    PersonWithIdPropertyOfTypeString p1 = new PersonWithIdPropertyOfTypeString();
    p1.setFirstName("Sven_1");
    p1.setAge(22);
    // insert
    mongoTemplate.insert(p1);
    // also try save
    mongoTemplate.save(p1);
    assertThat(p1.getId(), notNullValue());
    PersonWithIdPropertyOfTypeString p1q =
        mongoTemplate.findOne(
            new Query(where("id").is(p1.getId())), PersonWithIdPropertyOfTypeString.class);
    assertThat(p1q, notNullValue());
    assertThat(p1q.getId(), is(p1.getId()));
    checkCollectionContents(PersonWithIdPropertyOfTypeString.class, 1);

    // String id - provided
    PersonWithIdPropertyOfTypeString p2 = new PersonWithIdPropertyOfTypeString();
    p2.setFirstName("Sven_2");
    p2.setAge(22);
    p2.setId("TWO");
    // insert
    mongoTemplate.insert(p2);
    // also try save
    mongoTemplate.save(p2);
    assertThat(p2.getId(), notNullValue());
    PersonWithIdPropertyOfTypeString p2q =
        mongoTemplate.findOne(
            new Query(where("id").is(p2.getId())), PersonWithIdPropertyOfTypeString.class);
    assertThat(p2q, notNullValue());
    assertThat(p2q.getId(), is(p2.getId()));
    checkCollectionContents(PersonWithIdPropertyOfTypeString.class, 2);

    // String _id - generated
    PersonWith_idPropertyOfTypeString p3 = new PersonWith_idPropertyOfTypeString();
    p3.setFirstName("Sven_3");
    p3.setAge(22);
    // insert
    mongoTemplate.insert(p3);
    // also try save
    mongoTemplate.save(p3);
    assertThat(p3.get_id(), notNullValue());
    PersonWith_idPropertyOfTypeString p3q =
        mongoTemplate.findOne(
            new Query(where("_id").is(p3.get_id())), PersonWith_idPropertyOfTypeString.class);
    assertThat(p3q, notNullValue());
    assertThat(p3q.get_id(), is(p3.get_id()));
    checkCollectionContents(PersonWith_idPropertyOfTypeString.class, 1);

    // String _id - provided
    PersonWith_idPropertyOfTypeString p4 = new PersonWith_idPropertyOfTypeString();
    p4.setFirstName("Sven_4");
    p4.setAge(22);
    p4.set_id("FOUR");
    // insert
    mongoTemplate.insert(p4);
    // also try save
    mongoTemplate.save(p4);
    assertThat(p4.get_id(), notNullValue());
    PersonWith_idPropertyOfTypeString p4q =
        mongoTemplate.findOne(
            new Query(where("_id").is(p4.get_id())), PersonWith_idPropertyOfTypeString.class);
    assertThat(p4q, notNullValue());
    assertThat(p4q.get_id(), is(p4.get_id()));
    checkCollectionContents(PersonWith_idPropertyOfTypeString.class, 2);

    // ObjectId id - generated
    PersonWithIdPropertyOfTypeObjectId p5 = new PersonWithIdPropertyOfTypeObjectId();
    p5.setFirstName("Sven_5");
    p5.setAge(22);
    // insert
    mongoTemplate.insert(p5);
    // also try save
    mongoTemplate.save(p5);
    assertThat(p5.getId(), notNullValue());
    PersonWithIdPropertyOfTypeObjectId p5q =
        mongoTemplate.findOne(
            new Query(where("id").is(p5.getId())), PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(p5q, notNullValue());
    assertThat(p5q.getId(), is(p5.getId()));
    checkCollectionContents(PersonWithIdPropertyOfTypeObjectId.class, 1);

    // ObjectId id - provided
    PersonWithIdPropertyOfTypeObjectId p6 = new PersonWithIdPropertyOfTypeObjectId();
    p6.setFirstName("Sven_6");
    p6.setAge(22);
    p6.setId(new ObjectId());
    // insert
    mongoTemplate.insert(p6);
    // also try save
    mongoTemplate.save(p6);
    assertThat(p6.getId(), notNullValue());
    PersonWithIdPropertyOfTypeObjectId p6q =
        mongoTemplate.findOne(
            new Query(where("id").is(p6.getId())), PersonWithIdPropertyOfTypeObjectId.class);
    assertThat(p6q, notNullValue());
    assertThat(p6q.getId(), is(p6.getId()));
    checkCollectionContents(PersonWithIdPropertyOfTypeObjectId.class, 2);

    // ObjectId _id - generated
    PersonWith_idPropertyOfTypeObjectId p7 = new PersonWith_idPropertyOfTypeObjectId();
    p7.setFirstName("Sven_7");
    p7.setAge(22);
    // insert
    mongoTemplate.insert(p7);
    // also try save
    mongoTemplate.save(p7);
    assertThat(p7.get_id(), notNullValue());
    PersonWith_idPropertyOfTypeObjectId p7q =
        mongoTemplate.findOne(
            new Query(where("_id").is(p7.get_id())), PersonWith_idPropertyOfTypeObjectId.class);
    assertThat(p7q, notNullValue());
    assertThat(p7q.get_id(), is(p7.get_id()));
    checkCollectionContents(PersonWith_idPropertyOfTypeObjectId.class, 1);

    // ObjectId _id - provided
    PersonWith_idPropertyOfTypeObjectId p8 = new PersonWith_idPropertyOfTypeObjectId();
    p8.setFirstName("Sven_8");
    p8.setAge(22);
    p8.set_id(new ObjectId());
    // insert
    mongoTemplate.insert(p8);
    // also try save
    mongoTemplate.save(p8);
    assertThat(p8.get_id(), notNullValue());
    PersonWith_idPropertyOfTypeObjectId p8q =
        mongoTemplate.findOne(
            new Query(where("_id").is(p8.get_id())), PersonWith_idPropertyOfTypeObjectId.class);
    assertThat(p8q, notNullValue());
    assertThat(p8q.get_id(), is(p8.get_id()));
    checkCollectionContents(PersonWith_idPropertyOfTypeObjectId.class, 2);

    // Integer id - provided
    PersonWithIdPropertyOfTypeInteger p9 = new PersonWithIdPropertyOfTypeInteger();
    p9.setFirstName("Sven_9");
    p9.setAge(22);
    p9.setId(Integer.valueOf(12345));
    // insert
    mongoTemplate.insert(p9);
    // also try save
    mongoTemplate.save(p9);
    assertThat(p9.getId(), notNullValue());
    PersonWithIdPropertyOfTypeInteger p9q =
        mongoTemplate.findOne(
            new Query(where("id").in(p9.getId())), PersonWithIdPropertyOfTypeInteger.class);
    assertThat(p9q, notNullValue());
    assertThat(p9q.getId(), is(p9.getId()));
    checkCollectionContents(PersonWithIdPropertyOfTypeInteger.class, 1);

    // int id - provided
    PersonWithIdPropertyOfPrimitiveInt p10 = new PersonWithIdPropertyOfPrimitiveInt();
    p10.setFirstName("Sven_10");
    p10.setAge(22);
    p10.setId(12345);
    // insert
    mongoTemplate.insert(p10);
    // also try save
    mongoTemplate.save(p10);
    assertThat(p10.getId(), notNullValue());
    PersonWithIdPropertyOfPrimitiveInt p10q =
        mongoTemplate.findOne(
            new Query(where("id").in(p10.getId())), PersonWithIdPropertyOfPrimitiveInt.class);
    assertThat(p10q, notNullValue());
    assertThat(p10q.getId(), is(p10.getId()));
    checkCollectionContents(PersonWithIdPropertyOfPrimitiveInt.class, 1);

    // Long id - provided
    PersonWithIdPropertyOfTypeLong p11 = new PersonWithIdPropertyOfTypeLong();
    p11.setFirstName("Sven_9");
    p11.setAge(22);
    p11.setId(Long.valueOf(12345L));
    // insert
    mongoTemplate.insert(p11);
    // also try save
    mongoTemplate.save(p11);
    assertThat(p11.getId(), notNullValue());
    PersonWithIdPropertyOfTypeLong p11q =
        mongoTemplate.findOne(
            new Query(where("id").in(p11.getId())), PersonWithIdPropertyOfTypeLong.class);
    assertThat(p11q, notNullValue());
    assertThat(p11q.getId(), is(p11.getId()));
    checkCollectionContents(PersonWithIdPropertyOfTypeLong.class, 1);

    // long id - provided
    PersonWithIdPropertyOfPrimitiveLong p12 = new PersonWithIdPropertyOfPrimitiveLong();
    p12.setFirstName("Sven_10");
    p12.setAge(22);
    p12.setId(12345L);
    // insert
    mongoTemplate.insert(p12);
    // also try save
    mongoTemplate.save(p12);
    assertThat(p12.getId(), notNullValue());
    PersonWithIdPropertyOfPrimitiveLong p12q =
        mongoTemplate.findOne(
            new Query(where("id").in(p12.getId())), PersonWithIdPropertyOfPrimitiveLong.class);
    assertThat(p12q, notNullValue());
    assertThat(p12q.getId(), is(p12.getId()));
    checkCollectionContents(PersonWithIdPropertyOfPrimitiveLong.class, 1);
  }