Skip to content

Instantly share code, notes, and snippets.

@abhilater
Created June 22, 2019 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhilater/219e3078fa91d2901199360713dd0265 to your computer and use it in GitHub Desktop.
Save abhilater/219e3078fa91d2901199360713dd0265 to your computer and use it in GitHub Desktop.
/**
* Maps the {@link ResultSet} object from SQL query result to ORM Entity class.
* <p>
* For reflection cost optimization it uses the entity class fields list from
* {@link orm.cache.EntityFieldsCache} cache object.
* <p>
* It maps SQL data types to the Java data types as defined in the {@link ColumnField} annotations.
*/
public final class ResultSetToEntityMapper {
public static Object map(ResultSet rs, Class clazz, List<EntityField> fieldList) throws Exception {
Object entity = ConstructorUtils.invokeConstructor(clazz, null);
for (EntityField field : fieldList) {
field.getField().set(entity, getObjectValueFromResultSet(field.getFieldAnnotation(), rs));
}
return entity;
}
private static Object getObjectValueFromResultSet(ColumnField field, ResultSet rs) throws Exception {
ColumnField.FieldType fieldType = field.type();
try {
switch (fieldType) {
case BOOLEAN:
return rs.getBoolean(QueryUtil.getFieldName(field));
case INTEGER:
return rs.getInt(QueryUtil.getFieldName(field));
case LONG:
return rs.getLong(QueryUtil.getFieldName(field));
case STRING:
return rs.getString(QueryUtil.getFieldName(field));
case OBJECT:
return rs.getObject(QueryUtil.getFieldName(field)); // default value annotation field not required, can be replaced with fieldType in Entity class
default:
throw new RuntimeException("Field type not supported: " + fieldType);
}
} catch (SQLException se) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment