Beispiel #1
0
  public void testConvenienceMethodWithParam() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    List<Map<String, Object>> r = h.select("select * from something where id = ?", 1);
    assertEquals(1, r.size());
    assertEquals("eric", r.get(0).get("name"));
  }
Beispiel #2
0
  public void testFirstResult() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    Something r = h.createQuery("select * from something order by id").map(Something.class).first();

    assertNotNull(r);
    assertEquals("eric", r.getName());
  }
Beispiel #3
0
  public void testMappedQueryObject() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    Query<Something> query =
        h.createQuery("select * from something order by id").map(Something.class);

    List<Something> r = query.list();
    Something eric = r.get(0);
    assertEquals("eric", eric.getName());
    assertEquals(1, eric.getId());
  }
Beispiel #4
0
  public void testPositionalArgWithNamedParam() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    List<Something> r =
        h.createQuery("select * from something where name = :name")
            .bind(0, "eric")
            .map(Something.class)
            .list();

    assertEquals(1, r.size());
    assertEquals("eric", r.get(0).getName());
  }
Beispiel #5
0
  public void testMixedSetting() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    List<Something> r =
        h.createQuery("select * from something where name = :name and id = :id")
            .bind(0, "eric")
            .bind("id", 1)
            .map(Something.class)
            .list();

    assertEquals(1, r.size());
    assertEquals("eric", r.get(0).getName());
  }
Beispiel #6
0
  public void testIteratorBehavior2() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    ResultIterator<Something> i =
        h.createQuery("select * from something order by id").map(Something.class).iterator();

    Something first = i.next();
    assertEquals("eric", first.getName());
    Something second = i.next();
    assertEquals(2, second.getId());
    assertFalse(i.hasNext());

    i.close();
  }
Beispiel #7
0
  public void testMapper() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'brian')");

    Query<String> query =
        h.createQuery("select name from something order by id")
            .map(
                new ResultSetMapper<String>() {
                  public String map(int index, ResultSet r, StatementContext ctx)
                      throws SQLException {
                    return r.getString(1);
                  }
                });

    String name = query.list().get(0);
    assertEquals("eric", name);
  }
Beispiel #8
0
  public void testStatementCustomizersPersistAfterMap() throws Exception {
    h.insert("insert into something (id, name) values (?, ?)", 1, "hello");
    h.insert("insert into something (id, name) values (?, ?)", 2, "world");

    List<Something> rs =
        h.createQuery("select id, name from something").setMaxRows(1).map(Something.class).list();

    assertEquals(1, rs.size());
  }
Beispiel #9
0
  public void testMappedQueryObjectWithNullForPrimitiveIntField() throws Exception {
    h.insert("insert into something (id, name, intValue) values (1, 'eric', null)");

    Query<Something> query =
        h.createQuery("select * from something order by id").map(Something.class);

    List<Something> r = query.list();
    Something eric = r.get(0);
    assertEquals("eric", eric.getName());
    assertEquals(1, eric.getId());
    assertEquals(0, eric.getIntValue());
  }
Beispiel #10
0
  public void testIteratorBehavior3() throws Exception {
    h.insert("insert into something (id, name) values (1, 'eric')");
    h.insert("insert into something (id, name) values (2, 'eric')");

    int count = 0;
    for (Something s : h.createQuery("select * from something order by id").map(Something.class)) {
      count++;
      assertEquals("eric", s.getName());
    }

    assertEquals(2, count);
  }