From fa122cdfaeb398eb38117aa114bcfeb0e4802c16 Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Fri, 19 Apr 2013 00:55:11 +0000 Subject: [PATCH] OPENJPA-2373: special processing path for MapsId with auto-generated identity git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1469649 13f79535-47bb-0310-9956-ffa450edef68 --- .../meta/strats/RelationFieldStrategy.java | 20 ++++++++++++++++--- .../org/apache/openjpa/jdbc/sql/RowImpl.java | 1 + 2 files changed, 18 insertions(+), 3 deletions(-) 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 1d95ca228..3c0921e94 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 @@ -57,6 +57,7 @@ import org.apache.openjpa.jdbc.sql.SelectExecutor; import org.apache.openjpa.jdbc.sql.Union; import org.apache.openjpa.kernel.LockManager; import org.apache.openjpa.kernel.OpenJPAStateManager; +import org.apache.openjpa.kernel.StateManagerImpl; import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.meta.ClassMetaData; @@ -268,13 +269,26 @@ public class RelationFieldStrategy throws SQLException { if (field.getMappedBy() != null) return; - + Row row = null; OpenJPAStateManager rel = RelationStrategies.getStateManager (sm.fetchObjectField(field.getIndex()), store.getContext()); - if (field.getJoinDirection() == field.JOIN_INVERSE) + // Checks if the field being inserted is a MapsId field and + // the related object is using auto-assigned identity + // If the above conditions are satisfied and the related instance has + // already been inserted in the RowManger, then returns without further + // processing + if (sm instanceof StateManagerImpl) { + List mappedByIdFields = ((StateManagerImpl)sm).getMappedByIdFields(); + if (rel != null && ((ClassMapping)rel.getMetaData()).getTable().getAutoAssignedColumns().length > 0 + && mappedByIdFields!= null && mappedByIdFields.contains(field)) { + row = rm.getRow(((ClassMapping)rel.getMetaData()).getTable(), Row.ACTION_INSERT, rel, false); + if (row != null) return; + } + } + if (field.getJoinDirection() == FieldMapping.JOIN_INVERSE) updateInverse(sm, rel, store, rm); else { - Row row = field.getRow(sm, store, rm, Row.ACTION_INSERT); + if (row == null) row = field.getRow(sm, store, rm, Row.ACTION_INSERT); if (row != null && !field.isBiMTo1JT()) { field.setForeignKey(row, rel); // this is for bi-directional maps, the key and value of the diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java index e3d9a006c..794426443 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java @@ -897,6 +897,7 @@ public class RowImpl idx++; } } + setFlushed(true); } /**