@Test public void keyValueColumns() { h.execute("create table config (key varchar, value varchar)"); h.prepareBatch("insert into config (key, value) values (?, ?)") .add("foo", "123") .add("bar", "xyz") .execute(); // tag::keyValue[] Map<String, String> map = h.createQuery("select key, value from config") .setMapKeyColumn("key") .setMapValueColumn("value") .collectInto(new GenericType<Map<String, String>>() {}); // end::keyValue[] assertThat(map).containsOnly( entry("foo", "123"), entry("bar", "xyz")); }
@Test public void keyValueColumns() { h.execute("create table config (key varchar, value varchar)"); h.prepareBatch("insert into config (key, value) values (?, ?)") .add("foo", "123") .add("foo", "456") .add("bar", "xyz") .execute(); // tag::keyValue[] Multimap<String, String> map = h.createQuery("select key, value from config") .setMapKeyColumn("key") .setMapValueColumn("value") .collectInto(new GenericType<Multimap<String, String>>() {}); // end::keyValue[] Multimap<String, String> expected = ImmutableListMultimap.<String, String>builder() .putAll("foo", "123", "456") .put("bar", "xyz") .build(); assertThat(map).isEqualTo(expected); }
Map<User, Phone> map = handle.createQuery(sql) .setMapKeyColumn(null) .setMapValueColumn(null) .registerRowMapper(ConstructorMapper.factory(User.class, "u")) .registerRowMapper(ConstructorMapper.factory(Phone.class, "p"))
@Test public void keyValueColumns() { h.execute("create table config (key varchar, value varchar)"); h.prepareBatch("insert into config (key, value) values (?, ?)") .add("foo", "123") .add("foo", "456") .add("bar", "xyz") .execute(); // tag::keyValue[] Multimap<String, String> map = h.createQuery("select key, value from config") .setMapKeyColumn("key") .setMapValueColumn("value") .collectInto(new GenericType<Multimap<String, String>>() {}); // end::keyValue[] Multimap<String, String> expected = ImmutableListMultimap.<String, String>builder() .putAll("foo", "123", "456") .put("bar", "xyz") .build(); assertThat(map).isEqualTo(expected); }