Skip to content

Added new object writes - #4

Open
joshcbarnes wants to merge 2 commits into
replaceJdofrom
write
Open

joshcbarnes wants to merge 2 commits into
replaceJdofrom
write

Conversation

@joshcbarnes

Copy link
Copy Markdown
Owner

@jbedard

This isn't even done, but I was hoping to get some initial thoughts

argList.add(objectArgs);
}

super.getJdbcTemplate().batchUpdate(baseSql, argList);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bah...that's kind of lame

@joshcbarnes

Copy link
Copy Markdown
Owner Author

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we need a List?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later we want to get the Nth field...meaning we need to define an order at some point

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iteration order should still be the same as long as the Set doesn't change

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants