diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java index 89a48eefe..58bfbb4df 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ClassMapping.java @@ -175,6 +175,7 @@ public class ClassMapping FieldMapping fm; Joinable join; int pkIdx; + boolean canReadDiscriminator = true; for (int i = 0; i < pks.length; i++) { // we know that all pk column join mappings use primary key fields, // cause this mapping uses the oid as its primary key (we recursed @@ -182,7 +183,7 @@ public class ClassMapping join = assertJoinable(pks[i]); fm = getFieldMapping(join.getFieldIndex()); pkIdx = fm.getPrimaryKeyIndex(); - + canReadDiscriminator &= isSelfReference(fk, join.getColumns()); // could have already set value with previous multi-column joinable if (vals[pkIdx] == null) { res.startDataRequest(fm); @@ -197,8 +198,13 @@ public class ClassMapping // the oid data is loaded by the base type, but if discriminator data // is present, make sure to use it to construct the actual oid instance // so that we get the correct app id class, etc + + // Discriminator refers to the row but the vals[] may hold data that + // refer to another row. Then there is little point reading the disc + // value + ClassMapping dcls = cls; - if (subs) { + if (subs && canReadDiscriminator) { res.startDataRequest(cls.getDiscriminator()); try { Class dtype = cls.getDiscriminator().getClass(store, cls, res); @@ -217,6 +223,15 @@ public class ClassMapping } return oid; } + + boolean isSelfReference(ForeignKey fk, Column[] cols) { + if (fk == null) + return true; + for (Column col : cols) + if (fk.getColumn(col) != col) + return false; + return true; + } /** * Return the given column value(s) for the given object. The given 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 2e1c47d1f..e83db10f8 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 @@ -556,10 +556,8 @@ public class RelationFieldStrategy ClassMapping relMapping = field.getTypeMapping(); Object oid = null; if (relMapping.isMapped()) { - boolean subs = field.getPolymorphic() != ValueMapping.POLY_FALSE - && relMapping.getPCSubclasses().length > 0; oid = relMapping.getObjectId(store, res, field.getForeignKey(), - subs, null); + field.getPolymorphic() != ValueMapping.POLY_FALSE, null); } else { Column[] cols = field.getColumns(); if (relMapping.getIdentityType() == ClassMapping.ID_DATASTORE) {