Unbork handling of objects with no primary key. Added a comment indicating

where we will need to cache the results of a modifier (like an insert or an
update). Added hasPrimaryKey() to DepotMarshaller which we will soon use
externally.
This commit is contained in:
Michael Bayne
2006-09-26 19:41:40 +00:00
parent dafeeec087
commit dbe7d69162
2 changed files with 14 additions and 4 deletions
@@ -173,6 +173,14 @@ public class DepotMarshaller<T>
return _tableName;
}
/**
* Returns true if our persistent object defines a primary key.
*/
public boolean hasPrimaryKey ()
{
return (_primaryKey != null);
}
/**
* Returns a key configured with the primary key of the supplied object.
* Throws an exception if the persistent object did not declare a primary
@@ -180,7 +188,7 @@ public class DepotMarshaller<T>
*/
public DepotRepository.Key getPrimaryKey (Object object)
{
if (_primaryKey == null) {
if (!hasPrimaryKey()) {
throw new UnsupportedOperationException(
getClass().getName() + " does not define a primary key");
}
@@ -338,7 +346,7 @@ public class DepotMarshaller<T>
throws SQLException
{
// if we have no primary key or no generator, then we're done
if (_primaryKey == null || _keyGenerator == null) {
if (!hasPrimaryKey() || _keyGenerator == null) {
return;
}
@@ -140,7 +140,7 @@ public class DepotRepository
throws PersistenceException
{
final DepotMarshaller marsh = getMarshaller(record.getClass());
return invoke(new Modifier(marsh.getPrimaryKey(record)) {
return invoke(new Modifier(null) {
public int invoke (Connection conn) throws SQLException {
marsh.assignPrimaryKey(conn, record, false);
PreparedStatement stmt = marsh.createInsert(conn, record);
@@ -467,7 +467,9 @@ public class DepotRepository
// TODO: retry query on transient failure
Connection conn = _conprov.getConnection(getIdent(), false);
try {
return modifier.invoke(conn);
int result = modifier.invoke(conn);
// TODO: (optionally) cache the results of the modifier
return result;
} catch (SQLException sqe) {
// convert this exception to a DuplicateKeyException if appropriate