/** * Create a JoinRowMapper that maps each of the given types and returns a * {@link JoinRow} with the resulting values. * @param classes the types to extract * @return a JoinRowMapper that extracts the given types */ public static JoinRowMapper forTypes(Type... classes) { return new JoinRowMapper(classes); } }
@Override public JoinRow map(ResultSet r, StatementContext ctx) throws SQLException { return specialize(r, ctx).map(r, ctx); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { RegisterJoinRowMapper registerJoinRowMapper = (RegisterJoinRowMapper) annotation; registry.get(RowMappers.class).register(JoinRowMapper.forTypes(registerJoinRowMapper.value())); } }
@Test public void testCartesianProduct() { Multimap<User, Article> product = HashMultimap.create(); h.createQuery("SELECT * FROM user, article") .map(JoinRowMapper.forTypes(User.class, Article.class)) .forEach(jr -> product.put(jr.get(User.class), jr.get(Article.class))); Multimap<User, Article> expected = HashMultimap.create(); IntStream.rangeClosed(1, 3).forEach(u -> IntStream.rangeClosed(1, 3).forEach(a -> expected.put(u(u), a(a)))); assertThat(product).isEqualTo(expected); }
@Test public void testJoin() { // tag::multimap[] Multimap<User, Article> joined = HashMultimap.create(); h.createQuery("SELECT * FROM user NATURAL JOIN author NATURAL JOIN article") .map(JoinRowMapper.forTypes(User.class, Article.class)) .forEach(jr -> joined.put(jr.get(User.class), jr.get(Article.class))); // end::multimap[] assertThat(joined).isEqualTo(getExpected()); }
/** * Create a JoinRowMapper that maps each of the given types and returns a * {@link JoinRow} with the resulting values. * @param classes the types to extract * @return a JoinRowMapper that extracts the given types */ public static JoinRowMapper forTypes(Type... classes) { return new JoinRowMapper(classes); } }
@Test public void beanMapperPrefix() { handle.execute("create table contacts (id int, name text)"); handle.execute("create table phones (id int, contact_id int, name text, number text)"); handle.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice"); handle.execute("insert into phones (id, contact_id, name, number) values (?, ?, ?, ?)", 100, 1, "Home", "555-1212"); handle.execute("insert into phones (id, contact_id, name, number) values (?, ?, ?, ?)", 101, 1, "Work", "555-9999"); // tag::beanMapperPrefix[] handle.registerRowMapper(BeanMapper.factory(ContactBean.class, "c")); handle.registerRowMapper(BeanMapper.factory(PhoneBean.class, "p")); handle.registerRowMapper(JoinRowMapper.forTypes(ContactBean.class, PhoneBean.class)); List<JoinRow> contactPhones = handle.select("select " + "c.id cid, c.name cname, " + "p.id pid, p.name pname, p.number pnumber " + "from contacts c left join phones p on c.id = p.contact_id") .mapTo(JoinRow.class) .list(); // end::beanMapperPrefix[] assertThat(contactPhones) .extracting(cp -> cp.get(ContactBean.class), cp -> cp.get(PhoneBean.class)) .containsExactly( tuple(new ContactBean(1, "Alice"), new PhoneBean(100, "Home", "555-1212")), tuple(new ContactBean(1, "Alice"), new PhoneBean(101, "Work", "555-9999"))); } }