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")); }
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()); }
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()); }
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()); }
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()); }
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(); }
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); }
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()); }
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()); }
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); }