Search in sources :

Example 1 with NoSuchMapperException

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);
    });
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) Type(java.lang.reflect.Type) ElementTypeNotFoundException(org.jdbi.v3.core.collector.ElementTypeNotFoundException) Collector(java.util.stream.Collector) NoSuchCollectorException(org.jdbi.v3.core.collector.NoSuchCollectorException) NoSuchMapperException(org.jdbi.v3.core.mapper.NoSuchMapperException)

Example 2 with NoSuchMapperException

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;
    };
}
Also used : SQLException(java.sql.SQLException) List(java.util.List) Type(java.lang.reflect.Type) ResultSet(java.sql.ResultSet) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) JpaMember(org.jdbi.v3.jpa.internal.JpaMember) NoSuchMapperException(org.jdbi.v3.core.mapper.NoSuchMapperException) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) RowMapper(org.jdbi.v3.core.mapper.RowMapper) StatementContext(org.jdbi.v3.core.statement.StatementContext) JpaClass(org.jdbi.v3.jpa.internal.JpaClass) ArrayList(java.util.ArrayList) NoSuchMapperException(org.jdbi.v3.core.mapper.NoSuchMapperException) Type(java.lang.reflect.Type) JpaMember(org.jdbi.v3.jpa.internal.JpaMember)

Aggregations

Type (java.lang.reflect.Type)2 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)2 Constructor (java.lang.reflect.Constructor)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collector (java.util.stream.Collector)1 ElementTypeNotFoundException (org.jdbi.v3.core.collector.ElementTypeNotFoundException)1 NoSuchCollectorException (org.jdbi.v3.core.collector.NoSuchCollectorException)1 GenericType (org.jdbi.v3.core.generic.GenericType)1 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)1 RowMapper (org.jdbi.v3.core.mapper.RowMapper)1 StatementContext (org.jdbi.v3.core.statement.StatementContext)1 JpaClass (org.jdbi.v3.jpa.internal.JpaClass)1 JpaMember (org.jdbi.v3.jpa.internal.JpaMember)1