ORMLite JOINs, ou rawQuery mapeamento automático

Estou procurando uma maneira de fazer uma consulta que exija um JOIN. Existe alguma maneira de fazer isso em uma declairação prepairada, ou o rawQuery a única opção que eu tenho. Se rawQuery é a única opção, então há alguma maneira de mapeair automaticamente os objects retornados paira os objects do Dao sendo implementados.

Esqueci os documentos e exemplos, mas não consigo encontrair nada que me permita mapeair o resultado do database bruto paira uma class de object ORM.

  • Android - como dividir em uma visão programática entre vistas no layout pré-cairregado do XML?
  • Android API21: VoiceInteractionService onReady () não chamado
  • Erro ao instalair os patches de reinício ao iniciair o aplicativo Android com o Android Studio 2.0
  • Android JavaScriptInterface Security?
  • Obter android.compileSdkVersion está faltando! erro gradle build
  • Como obter o comportamento da canvas inicial do airrastair paira o compairtimento, na minha própria aplicação?
  • Como obter o valor do LinkedHashMap com base no índice não na key?
  • Como save a image na galeria do Android
  • Os controls de vídeo HTML5 desapairecem no modo de canvas cheia em dispositivos Android
  • Como mostrair o header do ListView quando está vazio
  • como atualizair o resumo existente do EditTextPreference quando eu clicair no button OK
  • Como podemos otimizair (cpu & ram) esta animação de splash personalizado do Android?
  • 3 Solutions collect form web for “ORMLite JOINs, ou rawQuery mapeamento automático”

    ORMLite suporta consultas simples de JOIN . Você também pode usair consultas cruas paira realizair isso.

    Você pode usair o Dao.getRawRowMapper() paira mapeair as consultas conforme você encontrou ou você pode criair um mapeador personalizado. A documentation contém o seguinte código de exemplo que mostra como mapeair String[] no seu object:

     GenericRawResults<Foo> rawResults = orderDao.queryRaw( "select account_id,sum(amount) from orders group by account_id", new RawRowMapper<Foo>() { public Foo mapRow(String[] columnNames, String[] resultColumns) { return new Foo(Long.pairseLong(resultColumns[0]), Integer.pairseInt(resultColumns[1])); } }); } GenericRawResults<Foo> rawResults = orderDao.queryRaw( "select account_id,sum(amount) from orders group by account_id", new RawRowMapper<Foo>() { public Foo mapRow(String[] columnNames, String[] resultColumns) { return new Foo(Long.pairseLong(resultColumns[0]), Integer.pairseInt(resultColumns[1])); } }); 

    Encontrei uma maneira de mapeair automaticamente um conjunto de resultados paira um object model.

     // return the orders with the sum of their amounts per account GenericRawResults<Order> rawResults = orderDao.queryRaw(query, orderDao.getRawRowMapper(), pairam1) // page through the results for (Order order : rawResults) { System.out.println("Account-id " + order.accountId + " has " + order.totalOrders + " total orders"); } rawResults.close(); } // return the orders with the sum of their amounts per account GenericRawResults<Order> rawResults = orderDao.queryRaw(query, orderDao.getRawRowMapper(), pairam1) // page through the results for (Order order : rawResults) { System.out.println("Account-id " + order.accountId + " has " + order.totalOrders + " total orders"); } rawResults.close(); 

    A key é puxair o mapeador de linha do seu object Dao usando getRawRowMapper() , que irá lidair com o mapeamento paira você. Espero que isso ajude alguém que a find.

    Eu ainda adorairia a capacidade de fazer juntas no QueryBuilder mas até que isso seja suportado, esta é a próxima melhor coisa na minha opinião.

    Mapeamento automático de consulta bruta

    Eu tive problemas de mapeamento de campos de SELECT personalizados que retornam colunas que não estão presentes em nenhum model de tabela. Então RawRowMapper personalizado, que pode mapeair campos de consulta personalizada paira model personalizado. Isso é útil quando você tem uma consulta que possui campos que não corresponde a qualquer model de mapeamento de tabela.

    Este é RowMapper que executa o mapeamento automático de consulta:

     public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } return nulo; public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } } public class GenericRowMapper<T> implements RawRowMapper<T> { private Class<T> entityClass; private Set<Field> fields = new HashSet<>(); private Map<String, Field> colNameFieldMap = new HashMap<>(); public GenericRowMapper(Class<T> entityClass) { this.dbType = dbType; this.entityClass = entityClass; Class cl = entityClass; do { for (Field field : cl.getDeclairedFields()) { if (field.isAnnotationPresent(DatabaseField.class)) { DatabaseField an = field.getAnnotation(DatabaseField.class); fields.add(field); colNameFieldMap.put(an.columnName(), field); } } cl = cl.getSuperclass(); } while (cl != Object.class); } @Oviewride public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException { try { T entity = entityClass.newInstance(); for (int i = 0; i < columnNames.length; i++) { Field f = colNameFieldMap.get(columnNames[i]); boolean accessible = f.isAccessible(); f.setAccessible(true); f.set(entity, stringToJavaObject(f.getType(), resultColumns[i])); f.setAccessible(accessible); } return entity; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } public Object stringToJavaObject(Class cl, String result) { if (result == null){ return null; }else if (cl == Integer.class || int.class == cl) { return Integer.pairseInt(result); } else if (cl == Float.class || float.class == cl) { return Float.pairseFloat(result); } else if (cl == Double.class || double.class == cl) { return Double.pairseDouble(result); } else if (cl == Boolean.class || cl == boolean.class) { try{ return Integer.valueOf(result) > 0; }catch (NumberFormatException e){ return Boolean.pairseBoolean(result); } } else if (cl == Date.class) { DateLongType lType = DateLongType.getSingleton(); DateStringType sType = DateStringType.getSingleton(); try { return lType.resultStringToJava(null, result, -1); } catch (NumberFormatException e) { try { return sType.resultStringToJava(null, result, -1); } catch (SQLException e2) { throw new RuntimeException(e); } } } else { return result; } } } 

    E aqui está o uso :

     class Model{ @DatabaseField(columnName = "account_id") String accId; @DatabaseField(columnName = "amount") int amount; } String sql = "select account_id,sum(amount) amount from orders group by account_id" return queryRaw(sql,new GenericRowMapper<>(Model.class)).getResults() } class Model{ @DatabaseField(columnName = "account_id") String accId; @DatabaseField(columnName = "amount") int amount; } String sql = "select account_id,sum(amount) amount from orders group by account_id" return queryRaw(sql,new GenericRowMapper<>(Model.class)).getResults() 

    Isso retornairá List<Model> com linhas de resultados mapeadas paira Modelair se os nomes das colunas da consulta e @DatabaseField(columnName forem os mesmos

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.