diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java index 569083949..8cc4a98ec 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerRelationMapTableFieldStrategy.java @@ -139,12 +139,19 @@ public class HandlerRelationMapTableFieldStrategy if (val.getTypeCode() != JavaTypes.PC || val.isEmbeddedPC()) throw new MetaDataException(_loc.get("not-relation", val)); FieldMapping mapped = field.getMappedByMapping(); - - if (mapped != null) // map to the owner table + String keyName = null; + _kio = new ColumnIO(); + if (mapped != null) { // map to the owner table + keyName = field.getName() + "_KEY"; + _kcols = HandlerStrategies.map(key, keyName, _kio, adapt); handleMappedBy(adapt); - else { + } else { // map to a separate table field.mapJoin(adapt, true); + DBDictionary dict = field.getMappingRepository().getDBDictionary(); + keyName = dict.getValidColumnName("key", field.getTable()); + _kcols = HandlerStrategies.map(key, keyName, _kio, adapt); + if (val.getTypeMapping().isMapped()) { ValueMappingInfo vinfo = val.getValueInfo(); ForeignKey fk = vinfo.getTypeJoin(val, "value", false, adapt); @@ -155,10 +162,6 @@ public class HandlerRelationMapTableFieldStrategy val.mapConstraints("value", adapt); } - _kio = new ColumnIO(); - DBDictionary dict = field.getMappingRepository().getDBDictionary(); - _kcols = HandlerStrategies.map(key, - dict.getValidColumnName("key", field.getTable()), _kio, adapt); field.mapPrimaryKey(adapt); } @@ -347,4 +350,11 @@ public class HandlerRelationMapTableFieldStrategy return HandlerStrategies.toDataStoreValue(field.getKeyMapping(), val, _kcols, store); } + + public void delete(OpenJPAStateManager sm, JDBCStore store, RowManager rm) + throws SQLException { + if (field.getMappedBy() != null) + return; + super.delete(sm, store, rm); + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/MapTableFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/MapTableFieldStrategy.java index 0b99563fb..51168ad86 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/MapTableFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/MapTableFieldStrategy.java @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.Map; import org.apache.openjpa.enhance.PersistenceCapable; +import org.apache.openjpa.enhance.ReflectingPersistenceCapable; import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration; import org.apache.openjpa.jdbc.kernel.JDBCStore; import org.apache.openjpa.jdbc.meta.ClassMapping; @@ -265,7 +266,10 @@ public abstract class MapTableFieldStrategy Collection entrySets = mapObj.entrySet(); boolean found = false; for (Map.Entry entry : entrySets) { - if (entry.getValue() == obj) { + Object value = entry.getValue(); + if (obj instanceof ReflectingPersistenceCapable) + obj = ((ReflectingPersistenceCapable)obj).getManagedInstance(); + if (value == obj) { Row newRow = (Row) ((RowImpl)row).clone(); Object keyObj = entry.getKey(); Strategy strat = fm.getStrategy(); diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java index 48bc6aac2..116c30cab 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Set; import org.apache.openjpa.enhance.PersistenceCapable; +import org.apache.openjpa.enhance.ReflectingPersistenceCapable; import org.apache.openjpa.jdbc.conf.JDBCConfiguration; import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration; import org.apache.openjpa.jdbc.kernel.JDBCFetchConfigurationImpl; @@ -276,6 +277,8 @@ public class RelationFieldStrategy } private Object getMapKeyObj(Map mapObj, Object value) { + if (value instanceof ReflectingPersistenceCapable) + value = ((ReflectingPersistenceCapable)value).getManagedInstance(); Set keySet = mapObj.keySet(); for (Object key : keySet) { if (mapObj.get(key) == value)