Skip to content

Commit f4ef461

Browse files
committed
fixes #118 Referencing parent instance does not work when columnPrefix is specified.
1 parent 7de9420 commit f4ef461

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class DefaultResultSetHandler implements ResultSetHandler {
7575
// nested resultmaps
7676
private final Map<CacheKey, Object> nestedResultObjects = new HashMap<CacheKey, Object>();
7777
private final Map<CacheKey, Object> ancestorObjects = new HashMap<CacheKey, Object>();
78+
private final Map<String, String> ancestorColumnPrefix = new HashMap<String, String>();
7879

7980
// multiple resultsets
8081
private final Map<String, ResultMapping> nextResultMaps = new HashMap<String, ResultMapping>();
@@ -215,6 +216,7 @@ private void closeResultSet(ResultSet rs) {
215216

216217
private void cleanUpAfterHandlingResultSet() {
217218
nestedResultObjects.clear();
219+
ancestorColumnPrefix.clear();
218220
}
219221

220222
private void validateResultMapsCount(ResultSetWrapper rsw, int resultMapCount) {
@@ -742,10 +744,11 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r
742744
//
743745

744746
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey combinedKey, CacheKey absoluteKey, String columnPrefix, Object partialObject) throws SQLException {
747+
final String resultMapId = resultMap.getId();
745748
Object resultObject = partialObject;
746749
if (resultObject != null) {
747750
final MetaObject metaObject = configuration.newMetaObject(resultObject);
748-
ancestorObjects.put(absoluteKey, resultObject);
751+
putAncestor(absoluteKey, resultObject, resultMapId, columnPrefix);
749752
applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, false);
750753
ancestorObjects.remove(absoluteKey);
751754
} else {
@@ -758,7 +761,7 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
758761
foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, columnPrefix) || foundValues;
759762
}
760763
foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, columnPrefix) || foundValues;
761-
ancestorObjects.put(absoluteKey, resultObject);
764+
putAncestor(absoluteKey, resultObject, resultMapId, columnPrefix);
762765
foundValues = applyNestedResultMappings(rsw, resultMap, metaObject, columnPrefix, combinedKey, true) || foundValues;
763766
ancestorObjects.remove(absoluteKey);
764767
foundValues = (lazyLoader != null && lazyLoader.size() > 0) || foundValues;
@@ -769,6 +772,13 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
769772
return resultObject;
770773
}
771774

775+
private void putAncestor(CacheKey rowKey, Object resultObject, String resultMapId, String columnPrefix) {
776+
if (!ancestorColumnPrefix.containsKey(resultMapId)) {
777+
ancestorColumnPrefix.put(resultMapId, columnPrefix);
778+
}
779+
ancestorObjects.put(rowKey, resultObject);
780+
}
781+
772782
//
773783
// NESTED RESULT MAP (JOIN MAPPING)
774784
//
@@ -781,11 +791,16 @@ private boolean applyNestedResultMappings(ResultSetWrapper rsw, ResultMap result
781791
try {
782792
final String columnPrefix = getColumnPrefix(parentPrefix, resultMapping);
783793
final ResultMap nestedResultMap = getNestedResultMap(rsw.getResultSet(), nestedResultMapId, columnPrefix);
784-
final CacheKey rowKey = createRowKey(nestedResultMap, rsw, columnPrefix);
785-
final Object ancestorObject = ancestorObjects.get(rowKey);
794+
CacheKey rowKey = null;
795+
Object ancestorObject = null;
796+
if (ancestorColumnPrefix.containsKey(nestedResultMapId)) {
797+
rowKey = createRowKey(nestedResultMap, rsw, ancestorColumnPrefix.get(nestedResultMapId));
798+
ancestorObject = ancestorObjects.get(rowKey);
799+
}
786800
if (ancestorObject != null) {
787801
if (newObject) metaObject.setValue(resultMapping.getProperty(), ancestorObject);
788802
} else {
803+
rowKey = createRowKey(nestedResultMap, rsw, columnPrefix);
789804
final CacheKey combinedKey = combineKeys(rowKey, parentRowKey);
790805
Object rowValue = nestedResultObjects.get(combinedKey);
791806
boolean knownValue = (rowValue != null);

src/test/java/org/apache/ibatis/submitted/parent_reference_3level/BlogTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2929
import org.junit.Assert;
3030
import org.junit.Before;
31-
import org.junit.Ignore;
3231
import org.junit.Test;
3332

3433
public class BlogTest {
@@ -99,7 +98,6 @@ public void testSelectBlogWithoutPosts() {
9998
}
10099
}
101100

102-
@Ignore
103101
@Test
104102
public void testSelectBlogWithPostsColumnPrefix() {
105103
SqlSession session = sqlSessionFactory.openSession();
@@ -120,7 +118,6 @@ public void testSelectBlogWithPostsColumnPrefix() {
120118
}
121119
}
122120

123-
@Ignore
124121
@Test
125122
public void testSelectBlogWithoutPostsColumnPrefix() {
126123
SqlSession session = sqlSessionFactory.openSession();

0 commit comments

Comments
 (0)