You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/01/24 00:45:38 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess AbstractPersistentField.java
arminw 2004/01/23 15:45:38
Modified: src/test/org/apache/ojb/broker NestedFieldsTest.java
src/java/org/apache/ojb/broker/metadata/fieldaccess
AbstractPersistentField.java
Log:
- Fix by Guillaume Nodet:
The problem arise when using nested objects that can be nulled.
Actually, when ojb reads the first field of a nested object that is
null, an instance is created to continue the recursive walk through
the nested objects, even if the given value to set for the attribute
is null. So i needed to make two modifications for the process to work.
- Add test for described problem
Revision Changes Path
1.5 +46 -1 db-ojb/src/test/org/apache/ojb/broker/NestedFieldsTest.java
Index: NestedFieldsTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/NestedFieldsTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- NestedFieldsTest.java 17 Oct 2003 14:12:55 -0000 1.4
+++ NestedFieldsTest.java 23 Jan 2004 23:45:38 -0000 1.5
@@ -108,6 +108,51 @@
assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
}
+ /**
+ * Not all nested fields were populated (some are 'null').
+ */
+ public void testStoreReadNestedFieldWithNullFields()
+ {
+ long timestamp = System.currentTimeMillis();
+ String entryName = "nested_entry_" + timestamp;
+
+ NestedDetail d = new NestedDetail(null);
+
+ ArrayList entryList = new ArrayList();
+ entryList.add(new NestedEntry(entryName));
+ entryList.add(new NestedEntry(entryName));
+ entryList.add(new NestedEntry(entryName));
+
+ d.setNestedEntryCollection(entryList);
+ NestedMain nested = new NestedMain("main_object_" + timestamp, d);
+
+ broker.beginTransaction();
+ broker.store(nested);
+ broker.commitTransaction();
+
+ Identity oid = new Identity(nested, broker);
+
+ assertNotNull(nested.getNestedDetail());
+ assertNull(nested.getNestedDetail().getNestedDetailDetail());
+ assertEquals(3, nested.getNestedDetail().getNestedEntryCollection().size());
+
+ // retrieve copy of nested object
+ // using cached version
+ NestedMain nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
+ assertNotNull(nestedCopy.getNestedDetail());
+ assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
+ assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
+ assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
+
+ // clear cache and retrieve copy of nested object
+ broker.clearCache();
+ nestedCopy = (NestedMain) broker.getObjectByIdentity(oid);
+ assertNotNull(nestedCopy.getNestedDetail());
+ assertNull(nestedCopy.getNestedDetail().getNestedDetailDetail());
+ assertNotNull(nestedCopy.getNestedDetail().getNestedEntryCollection());
+ assertEquals(3, nestedCopy.getNestedDetail().getNestedEntryCollection().size());
+ }
+
public void testUpdateNestedField()
{
long timestamp = System.currentTimeMillis();
@@ -338,7 +383,7 @@
public String toString()
{
- ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE);
+ ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
buf.append("realDetailName", realDetailName).
append("realDetailDescription", realDetailDescription);
return buf.toString();
1.11 +36 -30 db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/AbstractPersistentField.java
Index: AbstractPersistentField.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/AbstractPersistentField.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- AbstractPersistentField.java 9 Dec 2003 17:19:58 -0000 1.10
+++ AbstractPersistentField.java 23 Jan 2004 23:45:38 -0000 1.11
@@ -126,7 +126,10 @@
{
if (isNestedField())
{
- setNestedObject(targetObject, fieldName, value);
+ if (value != null || !getField().getType().isPrimitive())
+ {
+ setNestedObject(targetObject, fieldName, value);
+ }
}
else
{
@@ -314,38 +317,41 @@
PersistentField pField = createInternPersistentField(ProxyHelper.getRealClass(obj), name);
Object attrib = pField.get(ProxyHelper.getRealObject(obj));
- if (attrib == null)
+ if (attrib != null || value != null)
{
- try
- {
- attrib = pField.getType().newInstance();
- }
- catch (InstantiationException e)
- {
- throw new MetadataException("Error instantiate field: "
- + name + " in object:" + obj.getClass().getName(), e);
- }
- catch (IllegalAccessException e)
- {
- throw new MetadataException("Error getting field:"
- + name + " in object:" + obj.getClass().getName(), e);
- }
-
- /*
- TODO: here we need cast to AbstractPersistentField to execute the doSet-method.
- Find better solution without cast?
- */
- if (pField instanceof AbstractPersistentField)
- {
- ((AbstractPersistentField) pField).doSet(ProxyHelper.getRealObject(obj), attrib);
- }
- else
+ if (attrib == null)
{
- pField.set(ProxyHelper.getRealObject(obj), attrib);
+ try
+ {
+ attrib = pField.getType().newInstance();
+ }
+ catch (InstantiationException e)
+ {
+ throw new MetadataException("Error instantiate field: "
+ + name + " in object:" + obj.getClass().getName(), e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new MetadataException("Error getting field:"
+ + name + " in object:" + obj.getClass().getName(), e);
+ }
+
+ /*
+ TODO: here we need cast to AbstractPersistentField to execute the doSet-method.
+ Find better solution without cast?
+ */
+ if (pField instanceof AbstractPersistentField)
+ {
+ ((AbstractPersistentField) pField).doSet(ProxyHelper.getRealObject(obj), attrib);
+ }
+ else
+ {
+ pField.set(ProxyHelper.getRealObject(obj), attrib);
+ }
}
+ String nestedName = fieldName.substring(index + PATH_TOKEN.length());
+ setNestedObject(attrib, nestedName, value);
}
- String nestedName = fieldName.substring(index + PATH_TOKEN.length());
- setNestedObject(attrib, nestedName, value);
}
else
{
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org