From d189eac9dd6dd756643c3edce6932c56c87fb9a4 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Sat, 19 Dec 2009 04:50:24 +0000 Subject: [PATCH] OPENJPA-1438: fix wherePrimaryKey for entity using derived identity git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@892430 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/openjpa/jdbc/sql/RowImpl.java | 16 +++++++++++----- .../enhance/identity/TestMappedById.java | 4 ++++ 2 files changed, 15 insertions(+), 5 deletions(-) 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 d54ab68f4..185b5423e 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 @@ -183,7 +183,8 @@ public class RowImpl while (mapping.getTable() != getTable()) mapping = mapping.getPCSuperclassMapping(); Column[] cols = mapping.getPrimaryKeyColumns(); - flushJoinValues(sm, cols, cols, io, set); + Object oid = mapping.useIdClassFromParent() ? sm.getObjectId() : null; + flushJoinValues(sm, oid, cols, cols, io, set); } public void setForeignKey(ForeignKey fk, OpenJPAStateManager sm) @@ -219,7 +220,7 @@ public class RowImpl private void flushForeignKey(ForeignKey fk, ColumnIO io, OpenJPAStateManager sm, boolean set) throws SQLException { - flushJoinValues(sm, fk.getPrimaryKeyColumns(), fk.getColumns(), + flushJoinValues(sm, null, fk.getPrimaryKeyColumns(), fk.getColumns(), io, set); if (sm != null) { Column[] cols = fk.getConstantColumns(); @@ -250,7 +251,7 @@ public class RowImpl * @param set whether this should be flushed as an update or * as a where condition */ - private void flushJoinValues(OpenJPAStateManager to, Column[] toCols, + private void flushJoinValues(OpenJPAStateManager to, Object oid, Column[] toCols, Column[] fromCols, ColumnIO io, boolean set) throws SQLException { if (to == null) { @@ -278,8 +279,13 @@ public class RowImpl } join = toMapping.assertJoinable(toCols[i]); - val = join.getJoinValue(to, toCols[i], (JDBCStore) to. - getContext().getStoreManager().getInnermostDelegate()); + if (oid != null) + val = join.getJoinValue(oid, toCols[i], (JDBCStore) to. + getContext().getStoreManager().getInnermostDelegate()); + else + val = join.getJoinValue(to, toCols[i], (JDBCStore) to. + getContext().getStoreManager().getInnermostDelegate()); + if (set && val == null) { if (canSet(io, i, true)) setNull(fromCols[i]); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java index 10e6d95a9..4e299d91f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMappedById.java @@ -610,6 +610,10 @@ public class TestMappedById extends SingleEMFTestCase { MedicalHistory3 m = em.find(MedicalHistory3.class, new PersonId3("f_1", "l_1")); MedicalHistory3 m0 = medicals3.get("f_1"); assertEquals(m, m0); + + em.getTransaction().begin(); + em.remove(m); + em.getTransaction().commit(); } public void queryObj6() {