The following patch addresses Tuscany-122
Index: src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java =================================================================== --- src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java (revision 389875) +++ src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java (working copy) @@ -142,8 +142,9 @@ changes.add(setting.getProperty()); } else { Property ref = setting.getProperty(); - if ( !ref.isMany() ) { - RelationshipWrapper r = new RelationshipWrapper(mapping.getRelationshipByName(ref.getOpposite().getName())); + if ( !ref.isMany() ) { + RelationshipWrapper r = new RelationshipWrapper(mapping.getRelationshipByReference(ref)); + Iterator keys = r.getForeignKeys().iterator(); while ( keys.hasNext()) { String key = (String) keys.next(); Index: src/main/java/org/apache/tuscany/das/rdb/impl/DatabaseObject.java =================================================================== --- src/main/java/org/apache/tuscany/das/rdb/impl/DatabaseObject.java (revision 389875) +++ src/main/java/org/apache/tuscany/das/rdb/impl/DatabaseObject.java (working copy) @@ -81,6 +81,8 @@ Property parentRef = getParentReference(r.getPrimaryKeyTable()); DataObject parent = dataObject.getDataObject(parentRef); + if ( parent == null ) + return null; String parentKey = getParentKey(r, parameter); return parent.get(parentKey); Index: src/main/java/org/apache/tuscany/das/rdb/impl/ChangeSummarizer.java =================================================================== --- src/main/java/org/apache/tuscany/das/rdb/impl/ChangeSummarizer.java (revision 389875) +++ src/main/java/org/apache/tuscany/das/rdb/impl/ChangeSummarizer.java (working copy) @@ -16,6 +16,7 @@ */ package org.apache.tuscany.das.rdb.impl; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -24,9 +25,12 @@ import org.apache.tuscany.das.rdb.Key; import org.apache.tuscany.das.rdb.config.Column; import org.apache.tuscany.das.rdb.config.Config; +import org.apache.tuscany.das.rdb.config.Relationship; import org.apache.tuscany.das.rdb.config.Table; import org.apache.tuscany.das.rdb.config.wrapper.MappingWrapper; import org.apache.tuscany.das.rdb.config.wrapper.QualifiedColumn; +import org.apache.tuscany.das.rdb.config.wrapper.RelationshipWrapper; +import org.apache.tuscany.das.rdb.config.wrapper.TableWrapper; import org.apache.tuscany.das.rdb.util.DebugUtil; import org.apache.tuscany.sdo.impl.AttributeImpl; import org.apache.tuscany.sdo.impl.ChangeSummaryImpl; @@ -35,6 +39,7 @@ import commonj.sdo.ChangeSummary; import commonj.sdo.DataObject; +import commonj.sdo.Property; import commonj.sdo.Type; public class ChangeSummarizer { @@ -127,10 +132,9 @@ ReferenceImpl ref = (ReferenceImpl) setting.getFeature(); DebugUtil.debugln(getClass(), debug, ref.getName()); -// if (ref.getEOpposite().isMany()) { - if (referencesParent(ref)) { + if (hasState(ref, changedObject) ) { ChangeFactory factory = getRegistry().getFactory( - changedObject.getType()); + changedObject.getType()); changes.addUpdate(factory .createUpdateOperation(changedObject)); } @@ -142,6 +146,33 @@ } + private boolean hasState(Property ref, DataObject changedObject) { + if ( ref.getOpposite().isMany() ) { + return true; + } else { + MappingWrapper mw = this.mapping; + if ( mw.getConfig() == null ) + mw = registry.getFactory(changedObject.getType()).getConfig(); + if ( mw.getConfig() == null ) + return false; + + Relationship rel = mw.getRelationshipByReference(ref); + + if ( !rel.isMany()) { + // This is a one-one relationship + Table t = mapping.getTableByPropertyName(changedObject.getType().getName()); + TableWrapper tw = new TableWrapper(t); + RelationshipWrapper rw = new RelationshipWrapper(rel); + if (( rel.getForeignKeyTable().equals(t.getName())) && + ( Collections.disjoint(tw.getPrimaryKeyProperties(),rw.getForeignKeys()) )) + return true; + + } + + } + return false; + } + private boolean hasAttributeChange(List theChanges) { Iterator i = theChanges.iterator(); while (i.hasNext()) { @@ -224,14 +255,5 @@ } - private boolean referencesParent(ReferenceImpl reference) { - - if (reference.getEOpposite().isMany()) - return true; - -// if (!reference.getName().contains("_opposite")) -// return true; - - return false; - } + } Index: src/main/java/org/apache/tuscany/das/rdb/impl/ChangeFactory.java =================================================================== --- src/main/java/org/apache/tuscany/das/rdb/impl/ChangeFactory.java (revision 389875) +++ src/main/java/org/apache/tuscany/das/rdb/impl/ChangeFactory.java (working copy) @@ -168,5 +168,9 @@ return updateCommand; } + public MappingWrapper getConfig() { + return this.mapping; + } + } Index: src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java =================================================================== --- src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java (revision 389875) +++ src/main/java/org/apache/tuscany/das/rdb/config/wrapper/MappingWrapper.java (working copy) @@ -32,7 +32,9 @@ import org.apache.tuscany.das.rdb.config.impl.ConfigFactoryImpl; import org.apache.tuscany.das.rdb.util.DebugUtil; +import commonj.sdo.Property; + public class MappingWrapper { private static final ConfigFactory factory = ConfigFactoryImpl.eINSTANCE; @@ -368,6 +370,17 @@ return converters; } + public Relationship getRelationshipByReference(Property ref) { + Iterator i = config.getRelationship().iterator(); + while ( i.hasNext() ) { + Relationship r = (Relationship) i.next(); + if ( ref.getName().equals (r.getName()) || + ref.getOpposite().getName().equals(r.getName())) + return r; + } + throw new RuntimeException("Could not find relationship " + ref.getName() + " in the configuration"); + } + public Relationship getRelationshipByName(String name) { Iterator i = config.getRelationship().iterator(); while ( i.hasNext() ) {