Added new object writes - #4
joshcbarnes wants to merge 2 commits into
Conversation
| argList.add(objectArgs); | ||
| } | ||
|
|
||
| super.getJdbcTemplate().batchUpdate(baseSql, argList); |
There was a problem hiding this comment.
Past here I know I need to fetch the objects from the db to populate the ids...but I'm honestly not too sure how to do that. I feel like doing something like fetching the newest N rows would only work with the assumption of a repeatable read isolation level, but maybe we'd want to write this in a way so that we could change the isolation level? The only other way I can think to do this is basically to generate the ids on the java side. Any ideas how to make this work with autoincrement?
There was a problem hiding this comment.
Fetching the newest N rows also assumes that the natural ordering is the same as the insertion order. And fetching the last N rows is probably also pretty slow.
I think you have to insert each row one at a time, and fetch the ID after each insert?
Here's the plain mysql way: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id
And spring seems to have a wrapper way that might be a little more generic: http://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/jdbc.html#jdbc-auto-genereted-keys
There was a problem hiding this comment.
If we end up doing a fetch-id query after each insert query, maybe we may as well fetch the full row to also retrieve default DB column values?
INSERT INTO ... (..dirty-fields..) VALUES (...);
SELECT ..fields-not-in-^... FROM ... WHERE ID = LAST_INSERT_ID();Or something like that... Would that be too expensive compared to just SELECT LAST_INSERT_ID()?
Would be a lot nicer then forcing those defaults to be in two places (sql + java) or magically detecting those defaults with something such as DESCRIBE TABLE ... queries.
There was a problem hiding this comment.
bah...that's kind of lame
|
I've added some more, though this still isn't done. JDBC is turning out to be kind of frustrating to deal with. I was hoping for some ability to batch together a bunch of prepared statements, for example, but they only support either batching statements that all use the same base sql and only differ in params, or batching together raw sql. Frustrating... |
| private void persistDirty(List<PersistedObject> dirtyObjects, PersistenceInfo<PersistedObject> info) { | ||
| for (int i = 0; i < dirtyObjects.size(); i++) { | ||
| PersistedObject dirtyObject = dirtyObjects.get(i); | ||
| List<String> dirtyFields = new ArrayList<>(dirtyObject.getDirty()); |
There was a problem hiding this comment.
Any reason we need a List?
There was a problem hiding this comment.
Later we want to get the Nth field...meaning we need to define an order at some point
There was a problem hiding this comment.
The iteration order should still be the same as long as the Set doesn't change
There was a problem hiding this comment.
It doesn't help that this method is incomplete, but have a look at persistNew()'s prepared statement bit. We need to reference the position of the field - it isn't a case of iteration order
There was a problem hiding this comment.
I think you can still just use the assumption that iteration order will be the same. You might have to add int i =0 and i++ though...
@jbedard
This isn't even done, but I was hoping to get some initial thoughts