Hi there,
We experienced a problem with Castor in combination with MySQL: when a
table has "not null" int fields, and we're trying to insert null values,
the JDBC driver does not return an exception, but silently inserts a 0
(zero) value.
This behaviour breaks Castor's dirty checking since it assumes the field
still has got a "null" value.
We wanted to ensure there is no way we could accidently insert 0 values
into the database, so a coworker implemented the attached patch which
checks if a field is "required" in the Castor mapping before trying to
put it into the database.
Will you consider adding this extra check to Castor?
greets,
Klaasjan
Index: src/main/org/exolab/castor/persist/ClassMolder.java
===================================================================
RCS file: /cvs/castor/castor/src/main/org/exolab/castor/persist/ClassMolder.java,v
retrieving revision 1.74
diff -u -r1.74 ClassMolder.java
--- src/main/org/exolab/castor/persist/ClassMolder.java 6 Dec 2001 06:40:41 -0000 1.74
+++ src/main/org/exolab/castor/persist/ClassMolder.java 23 Apr 2002 12:40:51 -0000
@@ -1548,6 +1548,10 @@
switch (fieldType) {
case FieldMolder.PRIMITIVE:
newfields[i] = _fhs[i].getValue( object, tx.getClassLoader() );
+ if(newfields[i] == null && _fhs[i].isRequired())
+ {
+ throw new PersistenceException("\"" + _fhs[i].getFieldName() + "\" field is
+required ");
+ }
break;
case FieldMolder.SERIALIZABLE:
try {
@@ -1557,7 +1561,13 @@
ObjectOutputStream os = new ObjectOutputStream( bos );
os.writeObject( dependent );
newfields[i] = bos.toByteArray();
- } else {
+ }
+ else
+ {
+ if(_fhs[i].isRequired())
+ {
+ throw new PersistenceException("\"" + _fhs[i].getFieldName() + "\" field is
+required ");
+ }
newfields[i] = null;
}
} catch ( IOException e ) {
@@ -1571,7 +1581,13 @@
fieldEngine = _fhs[i].getFieldLockEngine();
value = _fhs[i].getValue( object, tx.getClassLoader() );
if ( value != null )
+ {
newfields[i] = fieldClassMolder.getIdentity( tx, value );
+ }
+ else if(_fhs[i].isRequired())
+ {
+ throw new PersistenceException("\"" + _fhs[i].getFieldName() + "\" field is
+required ");
+ }
}
break;
case FieldMolder.ONE_TO_MANY:
Index: src/main/org/exolab/castor/persist/FieldMolder.java
===================================================================
RCS file: /cvs/castor/castor/src/main/org/exolab/castor/persist/FieldMolder.java,v
retrieving revision 1.26
diff -u -r1.26 FieldMolder.java
--- src/main/org/exolab/castor/persist/FieldMolder.java 11 Dec 2001 10:34:09 -0000 1.26
+++ src/main/org/exolab/castor/persist/FieldMolder.java 23 Apr 2002 12:40:42 -0000
@@ -92,6 +92,8 @@
private boolean _multi;
+ private boolean _required;
+
private boolean _serial;
private boolean _addable;
@@ -180,6 +182,10 @@
return _multi;
}
+ public boolean isRequired() {
+ return _required;
+ }
+
public boolean isPersistanceCapable() {
return _fMold != null;
}
@@ -220,6 +226,11 @@
return _colClass;
}
+ public String getFieldName()
+ {
+ return _fieldName;
+ }
+
public Object getValue( Object object, ClassLoader loader ) {
Object value;
ReflectService rf = getContextReflectService ( loader );
@@ -432,7 +443,9 @@
// SatingService object as default
_defaultReflectService = new ReflectService();
_reflectServices = new HashMap();
-
+
+ _required = fieldMap.getRequired();
+
// Set enclosing ClassMolder
_eMold = eMold;