From 1b5ca789469979ba838e3efd144fa52c4b17f2de Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sat, 29 Mar 2008 23:19:42 +0000 Subject: [PATCH] Throw a useful error message when we are asked to create a primary key with mixed null and non-null values. --- .../jdbc/depot/DepotMarshaller.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index dd2df78..356ee50 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -351,26 +351,29 @@ public class DepotMarshaller try { Comparable[] values = new Comparable[_pkColumns.size()]; - boolean hasNulls = false; + int nulls = 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 || Integer.valueOf(0).equals(values[ii])) { - // if this is the first null we see but not the first field, freak out - if (!hasNulls && ii > 0) { - throw new IllegalArgumentException( - "Persistent object's primary key fields are mixed null and non-null."); - } - hasNulls = true; - } else if (hasNulls) { - // if this is a non-null field and we've previously seen nulls, also freak - throw new IllegalArgumentException( - "Persistent object's primary key fields are mixed null and non-null."); + if ((values[ii] = (Comparable)field.getField().get(object)) == null) { + nulls++; } } - // if all the fields were null, return null, else build a key - return hasNulls ? null : makePrimaryKey(values); + // make sure the keys are all null or all non-null + if (nulls == 0) { + return makePrimaryKey(values); + } else if (nulls == values.length) { + return null; + } + + // throw an informative error message + StringBuilder keys = new StringBuilder(); + for (int ii = 0; ii < _pkColumns.size(); ii++) { + keys.append(", ").append(_pkColumns.get(ii).getField().getName()); + keys.append("=").append(values[ii]); + } + throw new IllegalArgumentException("Primary key fields are mixed null and non-null " + + "[class=" + _pClass.getName() + keys + "]."); } catch (IllegalAccessException iae) { throw new RuntimeException(iae);