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:
@@ -340,24 +340,28 @@ public class DepotMarshaller<T>
|
|||||||
/**
|
/**
|
||||||
* Fills in the primary key just assigned to the supplied persistence
|
* Fills in the primary key just assigned to the supplied persistence
|
||||||
* object by the execution of the results of {@link #createInsert}.
|
* 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)
|
Connection conn, Object po, boolean postFactum)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// if we have no primary key or no generator, then we're done
|
// if we have no primary key or no generator, then we're done
|
||||||
if (!hasPrimaryKey() || _keyGenerator == null) {
|
if (!hasPrimaryKey() || _keyGenerator == null) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// run this generator either before or after the actual insertion
|
// run this generator either before or after the actual insertion
|
||||||
if (_keyGenerator.isPostFactum() != postFactum) {
|
if (_keyGenerator.isPostFactum() != postFactum) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int nextValue = _keyGenerator.nextGeneratedValue(conn);
|
int nextValue = _keyGenerator.nextGeneratedValue(conn);
|
||||||
_primaryKey.getField().set(po, nextValue);
|
_primaryKey.getField().set(po, nextValue);
|
||||||
|
return makePrimaryKey(nextValue);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
String errmsg = "Failed to assign primary key " +
|
String errmsg = "Failed to assign primary key " +
|
||||||
"[type=" + _pclass + "]";
|
"[type=" + _pclass + "]";
|
||||||
|
|||||||
@@ -142,11 +142,13 @@ public class DepotRepository
|
|||||||
final DepotMarshaller marsh = getMarshaller(record.getClass());
|
final DepotMarshaller marsh = getMarshaller(record.getClass());
|
||||||
return invoke(new Modifier(null) {
|
return invoke(new Modifier(null) {
|
||||||
public int invoke (Connection conn) throws SQLException {
|
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);
|
PreparedStatement stmt = marsh.createInsert(conn, record);
|
||||||
try {
|
try {
|
||||||
int mods = stmt.executeUpdate();
|
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;
|
return mods;
|
||||||
} finally {
|
} finally {
|
||||||
stmt.close();
|
stmt.close();
|
||||||
@@ -341,7 +343,8 @@ public class DepotRepository
|
|||||||
throws PersistenceException
|
throws PersistenceException
|
||||||
{
|
{
|
||||||
final DepotMarshaller marsh = getMarshaller(record.getClass());
|
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 {
|
public int invoke (Connection conn) throws SQLException {
|
||||||
PreparedStatement stmt = null;
|
PreparedStatement stmt = null;
|
||||||
try {
|
try {
|
||||||
@@ -358,10 +361,10 @@ public class DepotRepository
|
|||||||
|
|
||||||
// if the update modified zero rows or the primary key was
|
// if the update modified zero rows or the primary key was
|
||||||
// obviously unset, do an insertion
|
// obviously unset, do an insertion
|
||||||
marsh.assignPrimaryKey(conn, record, false);
|
updateKey(marsh.assignPrimaryKey(conn, record, false));
|
||||||
stmt = marsh.createInsert(conn, record);
|
stmt = marsh.createInsert(conn, record);
|
||||||
int mods = stmt.executeUpdate();
|
int mods = stmt.executeUpdate();
|
||||||
marsh.assignPrimaryKey(conn, record, true);
|
updateKey(marsh.assignPrimaryKey(conn, record, true));
|
||||||
return mods;
|
return mods;
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
@@ -584,6 +587,13 @@ public class DepotRepository
|
|||||||
_key = key;
|
_key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void updateKey (Key key)
|
||||||
|
{
|
||||||
|
if (key != null) {
|
||||||
|
_key = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected Key _key;
|
protected Key _key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user