From 7bb36afef0d3047822ae10f771d2419ca815e7b7 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 17 Oct 2007 00:42:17 +0000 Subject: [PATCH] Don't assign default values to columns that have a @GeneratedValue annotation because those will have their values set at insertion time and the database might rightly freak out about the inconsistency of saying that the field has a constant default value. --- .../samskivert/jdbc/depot/FieldMarshaller.java | 2 +- .../com/samskivert/jdbc/depot/SQLBuilder.java | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java b/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java index dd734c9..e989eae 100644 --- a/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java @@ -459,7 +459,7 @@ public abstract class FieldMarshaller protected Field _field; protected String _columnName; - ColumnDefinition _columnDefinition; + protected ColumnDefinition _columnDefinition; protected Computed _computed; protected GeneratedValue _generatedValue; } diff --git a/src/java/com/samskivert/jdbc/depot/SQLBuilder.java b/src/java/com/samskivert/jdbc/depot/SQLBuilder.java index ba74dac..e6acef7 100644 --- a/src/java/com/samskivert/jdbc/depot/SQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/SQLBuilder.java @@ -154,15 +154,16 @@ public abstract class SQLBuilder "Primitive Java type cannot be nullable [field=" + field.getName() + "]"); } - if (defaultValue == null) { + // Java primitive types cannot be null, so we provide a default value for these columns + // that matches Java's default for primitive types; however, if the column has a generated + // value, don't provide a default because that will anger the database Gods + if (defaultValue == null && genValue == null) { if (field.getType().equals(Byte.TYPE) || - field.getType().equals(Short.TYPE) || - field.getType().equals(Integer.TYPE) || - field.getType().equals(Long.TYPE) || - field.getType().equals(Float.TYPE) || - field.getType().equals(Double.TYPE)) { - // Java primitive types cannot be null, so we provide a default value for these - // columns that matches Java's default for primitive types + field.getType().equals(Short.TYPE) || + field.getType().equals(Integer.TYPE) || + field.getType().equals(Long.TYPE) || + field.getType().equals(Float.TYPE) || + field.getType().equals(Double.TYPE)) { defaultValue = "0"; } else if (field.getType().equals(Boolean.TYPE)) {