From eaa520044b93d0f44fdd8f34671b40680e8f2907 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 26 Sep 2006 19:41:40 +0000 Subject: [PATCH] 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. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1927 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/jdbc/depot/DepotMarshaller.java | 12 ++++++++++-- .../com/samskivert/jdbc/depot/DepotRepository.java | 6 ++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 1414fa93..bad2b012 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -173,6 +173,14 @@ public class DepotMarshaller 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 */ 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 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; } diff --git a/src/java/com/samskivert/jdbc/depot/DepotRepository.java b/src/java/com/samskivert/jdbc/depot/DepotRepository.java index 8c977cdc..bfc2a3a8 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotRepository.java +++ b/src/java/com/samskivert/jdbc/depot/DepotRepository.java @@ -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