From 2309c490f784dab0fef3edf5b6675137c1edea09 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 24 Oct 2008 05:13:28 +0000 Subject: [PATCH] Compromise on our 0 == null stance for primary key columns. See the code comments for further details. --- .../samskivert/jdbc/depot/DepotMarshaller.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 73f4c12..fc3112e 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -355,18 +355,24 @@ public class DepotMarshaller try { Comparable[] values = new Comparable[_pkColumns.size()]; - int nulls = 0; + int nulls = 0, zeros = 0; for (int ii = 0; ii < _pkColumns.size(); ii++) { FieldMarshaller field = _pkColumns.get(ii); values[ii] = (Comparable)field.getField().get(object); - if (values[ii] == null || - (values[ii] instanceof Number && ((Number)values[ii]).intValue() == 0)) { + if (values[ii] == null) { nulls++; + } else if (values[ii] instanceof Number && ((Number)values[ii]).intValue() == 0) { + nulls++; // zeros are considered nulls; see below + zeros++; } } - // make sure the keys are all null or all non-null - if (nulls == 0) { + // make sure the keys are all null or all non-null; we also allow primary keys where + // there are zero-valued primitive primary key columns as along as there is at least + // one non-zero valued additional key column; this is a compromise that allows sensible + // things like (id=99, type=0) but unfortunately also allows less sensible things like + // (id=0, type=5) while continuing to disallow the dangerous (id=0) + if (nulls == 0 || (nulls == zeros)) { return makePrimaryKey(values); } else if (nulls == values.length) { return null;