4 ms·
I tend to work more with document-like stores than SQL these days, so there may be more idiomatic solutions.... If you're complaining about having to update th
by fizx 14y ago
I tend to work more with document-like stores than SQL these days, so there may be more idiomatic solutions....
If you're complaining about having to update the fields in your object, I think thats kinda silly. You bought into the idea of static typing at some point, and now your data has type constraints. If you need need need flexibility you can of course make some field a Map<String, Object>.
If you're complaining about boilerplate, you can at least automate the mapping with code like:
class Util {
public static <T> T resultSetToObject(ResultSet rs, Class<T> klass) {
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()) {
Map<String, Object> columns = new LinkedHashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
columns.put(metaData.getColumnLabel(i), resultSet.getObject(i));
}
rows.add(columns);
}
return new ObjectMapper().convertValue(rows, klass)
}
}
- swah 14y agoYes, I wrote something similar (a Rowmap object)... But I have to admit it took me a while to realize that I could break that pattern.