use of org.jdbi.v3.core.mapper.NoSuchMapperException in project jdbi by jdbi.
the class ResultBearing method collectInto.
/**
* Collect the results into a container of the given type. A collector
* must be registered for the container type, which knows the element type
* for the container. A mapper must be registered for the element type.
* <p>
* This method is equivalent to {@code ResultBearing.mapTo(elementType).collect(containerCollector)}.
* </p>
*
* @param containerType the container type into which results will be collected
* @return a container into which result rows have been collected
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
default Object collectInto(Type containerType) {
return scanResultSet((rs, ctx) -> {
Collector collector = ctx.findCollectorFor(containerType).orElseThrow(() -> new NoSuchCollectorException("No collector registered for container type " + containerType));
Type elementType = ctx.findElementTypeFor(containerType).orElseThrow(() -> new ElementTypeNotFoundException("Unknown element type for container type " + containerType));
RowMapper<?> mapper = ctx.findMapperFor(elementType).orElseThrow(() -> new NoSuchMapperException("No mapper registered for element type " + elementType));
return ResultIterable.of(rs, mapper, ctx).collect(collector);
});
}
use of org.jdbi.v3.core.mapper.NoSuchMapperException in project jdbi by jdbi.
the class JpaMapper method specialize.
@Override
public RowMapper<C> specialize(ResultSet rs, StatementContext ctx) throws SQLException {
Constructor<C> constructor;
try {
constructor = clazz.getDeclaredConstructor();
} catch (ReflectiveOperationException e) {
throw new EntityMemberAccessException("Unable to get constructor for " + clazz, e);
}
constructor.setAccessible(true);
List<MemberSetter<C>> setters = new ArrayList<>();
for (int colIndex = rs.getMetaData().getColumnCount(); colIndex >= 1; colIndex--) {
String columnLabel = rs.getMetaData().getColumnLabel(colIndex);
JpaMember member = jpaClass.lookupMember(columnLabel);
if (member != null) {
Type memberType = member.getType();
ColumnMapper<?> columnMapper = ctx.findColumnMapperFor(memberType).orElseThrow(() -> new NoSuchMapperException("No column mapper for " + memberType));
final int columnIndex = colIndex;
setters.add(obj -> member.write(obj, columnMapper.map(rs, columnIndex, ctx)));
}
}
return (r, c) -> {
C obj;
try {
obj = constructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new EntityMemberAccessException("Unable to invoke " + constructor, e);
}
for (MemberSetter<C> setter : setters) {
setter.mapAndSetMember(obj);
}
return obj;
};
}
Aggregations