Compromise on our 0 == null stance for primary key columns. See the code
comments for further details.
This commit is contained in:
@@ -355,18 +355,24 @@ public class DepotMarshaller<T extends PersistentRecord>
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Comparable<?>[] values = new Comparable<?>[_pkColumns.size()];
|
Comparable<?>[] values = new Comparable<?>[_pkColumns.size()];
|
||||||
int nulls = 0;
|
int nulls = 0, zeros = 0;
|
||||||
for (int ii = 0; ii < _pkColumns.size(); ii++) {
|
for (int ii = 0; ii < _pkColumns.size(); ii++) {
|
||||||
FieldMarshaller<?> field = _pkColumns.get(ii);
|
FieldMarshaller<?> field = _pkColumns.get(ii);
|
||||||
values[ii] = (Comparable<?>)field.getField().get(object);
|
values[ii] = (Comparable<?>)field.getField().get(object);
|
||||||
if (values[ii] == null ||
|
if (values[ii] == null) {
|
||||||
(values[ii] instanceof Number && ((Number)values[ii]).intValue() == 0)) {
|
|
||||||
nulls++;
|
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
|
// make sure the keys are all null or all non-null; we also allow primary keys where
|
||||||
if (nulls == 0) {
|
// 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);
|
return makePrimaryKey(values);
|
||||||
} else if (nulls == values.length) {
|
} else if (nulls == values.length) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user