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:
@@ -173,6 +173,14 @@ public class DepotMarshaller<T>
|
|||||||
return _tableName;
|
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.
|
* Returns a key configured with the primary key of the supplied object.
|
||||||
* Throws an exception if the persistent object did not declare a primary
|
* 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)
|
public DepotRepository.Key getPrimaryKey (Object object)
|
||||||
{
|
{
|
||||||
if (_primaryKey == null) {
|
if (!hasPrimaryKey()) {
|
||||||
throw new UnsupportedOperationException(
|
throw new UnsupportedOperationException(
|
||||||
getClass().getName() + " does not define a primary key");
|
getClass().getName() + " does not define a primary key");
|
||||||
}
|
}
|
||||||
@@ -338,7 +346,7 @@ public class DepotMarshaller<T>
|
|||||||
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 (_primaryKey == null || _keyGenerator == null) {
|
if (!hasPrimaryKey() || _keyGenerator == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ 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)) {
|
return invoke(new Modifier(null) {
|
||||||
public int invoke (Connection conn) throws SQLException {
|
public int invoke (Connection conn) throws SQLException {
|
||||||
marsh.assignPrimaryKey(conn, record, false);
|
marsh.assignPrimaryKey(conn, record, false);
|
||||||
PreparedStatement stmt = marsh.createInsert(conn, record);
|
PreparedStatement stmt = marsh.createInsert(conn, record);
|
||||||
@@ -467,7 +467,9 @@ public class DepotRepository
|
|||||||
// TODO: retry query on transient failure
|
// TODO: retry query on transient failure
|
||||||
Connection conn = _conprov.getConnection(getIdent(), false);
|
Connection conn = _conprov.getConnection(getIdent(), false);
|
||||||
try {
|
try {
|
||||||
return modifier.invoke(conn);
|
int result = modifier.invoke(conn);
|
||||||
|
// TODO: (optionally) cache the results of the modifier
|
||||||
|
return result;
|
||||||
|
|
||||||
} catch (SQLException sqe) {
|
} catch (SQLException sqe) {
|
||||||
// convert this exception to a DuplicateKeyException if appropriate
|
// convert this exception to a DuplicateKeyException if appropriate
|
||||||
|
|||||||
Reference in New Issue
Block a user