Update our Modifier's key on an insert (and store) so that it can (eventually)

properly cache the results of our insert based on the newly assigned primary
key. Also avoid freakout if an attempt is made to store() a non-primary-key
using object.
This commit is contained in:
Michael Bayne
2006-09-26 19:51:58 +00:00
parent dbe7d69162
commit b9b199b725
2 changed files with 22 additions and 8 deletions
@@ -340,24 +340,28 @@ public class DepotMarshaller<T>
/**
* Fills in the primary key just assigned to the supplied persistence
* object by the execution of the results of {@link #createInsert}.
*
* @return the newly assigned primary key or null if the object does not
* use primary keys or this is not the right time to assign the key.
*/
public void assignPrimaryKey (
public DepotRepository.Key assignPrimaryKey (
Connection conn, Object po, boolean postFactum)
throws SQLException
{
// if we have no primary key or no generator, then we're done
if (!hasPrimaryKey() || _keyGenerator == null) {
return;
return null;
}
// run this generator either before or after the actual insertion
if (_keyGenerator.isPostFactum() != postFactum) {
return;
return null;
}
try {
int nextValue = _keyGenerator.nextGeneratedValue(conn);
_primaryKey.getField().set(po, nextValue);
return makePrimaryKey(nextValue);
} catch (Exception e) {
String errmsg = "Failed to assign primary key " +
"[type=" + _pclass + "]";
@@ -142,11 +142,13 @@ public class DepotRepository
final DepotMarshaller marsh = getMarshaller(record.getClass());
return invoke(new Modifier(null) {
public int invoke (Connection conn) throws SQLException {
marsh.assignPrimaryKey(conn, record, false);
// update our modifier's key so that it can cache our results
updateKey(marsh.assignPrimaryKey(conn, record, false));
PreparedStatement stmt = marsh.createInsert(conn, record);
try {
int mods = stmt.executeUpdate();
marsh.assignPrimaryKey(conn, record, true);
// check again in case we have a post-factum key generator
updateKey(marsh.assignPrimaryKey(conn, record, true));
return mods;
} finally {
stmt.close();
@@ -341,7 +343,8 @@ public class DepotRepository
throws PersistenceException
{
final DepotMarshaller marsh = getMarshaller(record.getClass());
return invoke(new Modifier(marsh.getPrimaryKey(record)) {
Key key = marsh.hasPrimaryKey() ? marsh.getPrimaryKey(record) : null;
return invoke(new Modifier(key) {
public int invoke (Connection conn) throws SQLException {
PreparedStatement stmt = null;
try {
@@ -358,10 +361,10 @@ public class DepotRepository
// if the update modified zero rows or the primary key was
// obviously unset, do an insertion
marsh.assignPrimaryKey(conn, record, false);
updateKey(marsh.assignPrimaryKey(conn, record, false));
stmt = marsh.createInsert(conn, record);
int mods = stmt.executeUpdate();
marsh.assignPrimaryKey(conn, record, true);
updateKey(marsh.assignPrimaryKey(conn, record, true));
return mods;
} finally {
@@ -584,6 +587,13 @@ public class DepotRepository
_key = key;
}
protected void updateKey (Key key)
{
if (key != null) {
_key = key;
}
}
protected Key _key;
}