Re:Why isn't Java used for modern web application development?
by fizx on Sunday June 10, 2012 @05:21PM
(#4092253)
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)
}
}
Now in Java/Dropwizard the tendency is to map from SQL rows to objects, then from objects to JSON. Which is a PITA, even more when your design/database changes too much. (The upside is that when you get some object, the data IS there).