Search in sources :

Example 1 with JpaMember

use of org.jdbi.v3.jpa.internal.JpaMember 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)

Example 2 with JpaMember

use of org.jdbi.v3.jpa.internal.JpaMember in project jdbi by jdbi.

the class BindJpaFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    BindJpa bind = (BindJpa) annotation;
    final String prefix;
    if (bind.value().isEmpty()) {
        prefix = "";
    } else {
        prefix = bind.value() + ".";
    }
    return (stmt, arg) -> {
        JpaClass<?> jpaClass = JpaClass.get(arg.getClass());
        for (JpaMember member : jpaClass.members()) {
            stmt.bindByType(prefix + member.getColumnName(), readMember(arg, member), member.getType());
        }
    };
}
Also used : EntityMemberAccessException(org.jdbi.v3.jpa.EntityMemberAccessException) BindJpa(org.jdbi.v3.jpa.BindJpa) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) SqlStatementCustomizerFactory(org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory) Annotation(java.lang.annotation.Annotation) SqlStatementParameterCustomizer(org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BindJpa(org.jdbi.v3.jpa.BindJpa)

Aggregations

Type (java.lang.reflect.Type)2 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)1 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)1 RowMapper (org.jdbi.v3.core.mapper.RowMapper)1 StatementContext (org.jdbi.v3.core.statement.StatementContext)1 BindJpa (org.jdbi.v3.jpa.BindJpa)1 EntityMemberAccessException (org.jdbi.v3.jpa.EntityMemberAccessException)1 JpaClass (org.jdbi.v3.jpa.internal.JpaClass)1 JpaMember (org.jdbi.v3.jpa.internal.JpaMember)1 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)1 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)1