3 ms·
I'm kinda testing the same setup. There are a couple nice things, but one thing that hurts me coming from Python is that in Python an SQL row was just an dict w
by swah 14y ago
I'm kinda testing the same setup. There are a couple nice things, but one thing that hurts me coming from Python is that in Python an SQL row was just an dict with data, and I could change my schema (and add/remove entries from this object) and then return it as JSON quite easily.
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).
- fizx 14y agoI 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.