1
1
/*
2
- * Copyright 2009-2012 The MyBatis Team
2
+ * Copyright 2009-2013 The MyBatis Team
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
42
42
43
43
public class NestedResultSetHandler extends FastResultSetHandler {
44
44
45
- private final Map <CacheKey , Object > objectCache ;
46
- private final Map <CacheKey , Object > ancestorCache ;
45
+ private final Map <CacheKey , Object > objectCache = new HashMap < CacheKey , Object >() ;
46
+ private final Map <CacheKey , Object > ancestorCache = new HashMap < CacheKey , Object >() ;
47
47
48
48
public NestedResultSetHandler (Executor executor , MappedStatement mappedStatement , ParameterHandler parameterHandler , ResultHandler resultHandler , BoundSql boundSql , RowBounds rowBounds ) {
49
49
super (executor , mappedStatement , parameterHandler , resultHandler , boundSql , rowBounds );
50
- objectCache = new HashMap <CacheKey , Object >();
51
- ancestorCache = new HashMap <CacheKey , Object >();
52
50
if (configuration .isSafeRowBoundsEnabled ()) {
53
51
ensureNoRowBounds (rowBounds );
54
52
}
55
53
}
56
54
57
55
private void ensureNoRowBounds (RowBounds rowBounds ) {
58
- if (rowBounds != null
59
- && (rowBounds .getLimit () < RowBounds .NO_ROW_LIMIT
60
- || rowBounds .getOffset () > RowBounds .NO_ROW_OFFSET )) {
56
+ if (rowBounds != null && (rowBounds .getLimit () < RowBounds .NO_ROW_LIMIT || rowBounds .getOffset () > RowBounds .NO_ROW_OFFSET )) {
61
57
throw new ExecutorException ("Mapped Statements with nested result mappings cannot be safely constrained by RowBounds. "
62
58
+ "Use safeRowBoundsEnabled=false setting to bypass this check." );
63
59
}
@@ -105,36 +101,36 @@ protected Object getRowValue(ResultSet rs, ResultMap resultMap, CacheKey rowKey,
105
101
}
106
102
107
103
protected Object getRowValue (ResultSet rs , ResultMap resultMap , CacheKey combinedKey , CacheKey absoluteKey , String columnPrefix , ResultColumnCache resultColumnCache ) throws SQLException {
108
- if (ancestorCache .containsKey (absoluteKey )) {
109
- return ancestorCache .get (absoluteKey );
110
- } else if (objectCache .containsKey (combinedKey )) {
111
- final Object resultObject = objectCache .get (combinedKey );
112
- ancestorCache .put (absoluteKey , resultObject );
113
- final MetaObject metaObject = configuration .newMetaObject (resultObject );
114
- applyNestedResultMappings (rs , resultMap , metaObject , columnPrefix , resultColumnCache , combinedKey , false );
115
- ancestorCache .remove (absoluteKey );
116
- return resultObject ;
117
- } else {
118
- final ResultLoaderMap lazyLoader = instantiateResultLoaderMap ();
119
- Object resultObject = createResultObject (rs , resultMap , lazyLoader , columnPrefix , resultColumnCache );
120
- if (resultObject != null && !typeHandlerRegistry .hasTypeHandler (resultMap .getType ())) {
104
+ Object resultObject = ancestorCache .get (absoluteKey );
105
+ if (resultObject == null ) {
106
+ resultObject = objectCache .get (combinedKey );
107
+ if (resultObject != null ) {
121
108
ancestorCache .put (absoluteKey , resultObject );
122
109
final MetaObject metaObject = configuration .newMetaObject (resultObject );
123
- boolean foundValues = resultMap .getConstructorResultMappings ().size () > 0 ;
124
- if (shouldApplyAutomaticMappings (resultMap , AutoMappingBehavior .FULL .equals (configuration .getAutoMappingBehavior ()))) {
125
- final List <String > unmappedColumnNames = resultColumnCache .getUnmappedColumnNames (resultMap , columnPrefix );
126
- foundValues = applyAutomaticMappings (rs , unmappedColumnNames , metaObject , columnPrefix , resultColumnCache ) || foundValues ;
127
- }
128
- final List <String > mappedColumnNames = resultColumnCache .getMappedColumnNames (resultMap , columnPrefix );
129
- foundValues = applyPropertyMappings (rs , resultMap , mappedColumnNames , metaObject , lazyLoader , columnPrefix ) || foundValues ;
130
- foundValues = applyNestedResultMappings (rs , resultMap , metaObject , columnPrefix , resultColumnCache , combinedKey , true ) || foundValues ;
131
- foundValues = (lazyLoader != null && lazyLoader .size () > 0 ) || foundValues ;
132
- resultObject = foundValues ? resultObject : null ;
110
+ applyNestedResultMappings (rs , resultMap , metaObject , columnPrefix , resultColumnCache , combinedKey , false );
133
111
ancestorCache .remove (absoluteKey );
112
+ } else {
113
+ final ResultLoaderMap lazyLoader = instantiateResultLoaderMap ();
114
+ resultObject = createResultObject (rs , resultMap , lazyLoader , columnPrefix , resultColumnCache );
115
+ if (resultObject != null && !typeHandlerRegistry .hasTypeHandler (resultMap .getType ())) {
116
+ ancestorCache .put (absoluteKey , resultObject );
117
+ final MetaObject metaObject = configuration .newMetaObject (resultObject );
118
+ boolean foundValues = resultMap .getConstructorResultMappings ().size () > 0 ;
119
+ if (shouldApplyAutomaticMappings (resultMap , AutoMappingBehavior .FULL .equals (configuration .getAutoMappingBehavior ()))) {
120
+ final List <String > unmappedColumnNames = resultColumnCache .getUnmappedColumnNames (resultMap , columnPrefix );
121
+ foundValues = applyAutomaticMappings (rs , unmappedColumnNames , metaObject , columnPrefix , resultColumnCache ) || foundValues ;
122
+ }
123
+ final List <String > mappedColumnNames = resultColumnCache .getMappedColumnNames (resultMap , columnPrefix );
124
+ foundValues = applyPropertyMappings (rs , resultMap , mappedColumnNames , metaObject , lazyLoader , columnPrefix ) || foundValues ;
125
+ foundValues = applyNestedResultMappings (rs , resultMap , metaObject , columnPrefix , resultColumnCache , combinedKey , true ) || foundValues ;
126
+ foundValues = (lazyLoader != null && lazyLoader .size () > 0 ) || foundValues ;
127
+ resultObject = foundValues ? resultObject : null ;
128
+ ancestorCache .remove (absoluteKey );
129
+ }
130
+ if (combinedKey != CacheKey .NULL_CACHE_KEY ) objectCache .put (combinedKey , resultObject );
134
131
}
135
- if (combinedKey .getUpdateCount () > 1 ) objectCache .put (combinedKey , resultObject );
136
- return resultObject ;
137
132
}
133
+ return resultObject ;
138
134
}
139
135
140
136
//
@@ -153,7 +149,7 @@ private boolean applyNestedResultMappings(ResultSet rs, ResultMap resultMap, Met
153
149
final String columnPrefix = columnPrefixBuilder .length () == 0 ? null : columnPrefixBuilder .toString ().toUpperCase (Locale .ENGLISH );
154
150
final ResultMap nestedResultMap = getNestedResultMap (rs , nestedResultMapId , columnPrefix );
155
151
final CacheKey absoluteKey = createAbsoluteKey (nestedResultMap , rs , columnPrefix , resultColumnCache );
156
- final CacheKey combinedKey = getCombinedKey (absoluteKey , parentRowKey );
152
+ final CacheKey combinedKey = combineKeys (absoluteKey , parentRowKey );
157
153
final boolean knownValue = objectCache .containsKey (combinedKey );
158
154
final boolean isAncestor = ancestorCache .containsKey (absoluteKey );
159
155
Object rowValue = getRowValue (rs , nestedResultMap , combinedKey , absoluteKey , columnPrefix , resultColumnCache );
@@ -245,8 +241,8 @@ private CacheKey createAbsoluteKey(ResultMap resultMap, ResultSet rs, String col
245
241
}
246
242
return cacheKey ;
247
243
}
248
-
249
- private CacheKey getCombinedKey (CacheKey rowKey , CacheKey parentRowKey ) {
244
+
245
+ private CacheKey combineKeys (CacheKey rowKey , CacheKey parentRowKey ) {
250
246
if (rowKey .getUpdateCount () > 1 && parentRowKey .getUpdateCount () > 1 ) {
251
247
CacheKey combinedKey ;
252
248
try {
@@ -268,7 +264,13 @@ private List<ResultMapping> getResultMappingsForRowKey(ResultMap resultMap) {
268
264
return resultMappings ;
269
265
}
270
266
271
- private void createRowKeyForMappedProperties (ResultMap resultMap , ResultSet rs , CacheKey cacheKey , List <ResultMapping > resultMappings , String columnPrefix , ResultColumnCache resultColumnCache ) throws SQLException {
267
+ private void createRowKeyForMappedProperties (
268
+ ResultMap resultMap ,
269
+ ResultSet rs ,
270
+ CacheKey cacheKey ,
271
+ List <ResultMapping > resultMappings ,
272
+ String columnPrefix ,
273
+ ResultColumnCache resultColumnCache ) throws SQLException {
272
274
for (ResultMapping resultMapping : resultMappings ) {
273
275
if (resultMapping .getNestedResultMapId () != null ) {
274
276
final ResultMap nestedResultMap = configuration .getResultMap (resultMapping .getNestedResultMapId ());
@@ -278,7 +280,7 @@ private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSet rs,
278
280
final String column = prependPrefix (resultMapping .getColumn (), columnPrefix );
279
281
final TypeHandler <?> th = resultMapping .getTypeHandler ();
280
282
List <String > mappedColumnNames = resultColumnCache .getMappedColumnNames (resultMap , columnPrefix );
281
- if (column != null && mappedColumnNames .contains (column .toUpperCase (Locale .ENGLISH ))) { // issue #114
283
+ if (column != null && mappedColumnNames .contains (column .toUpperCase (Locale .ENGLISH ))) { // Issue #114
282
284
final Object value = th .getResult (rs , column );
283
285
if (value != null ) {
284
286
cacheKey .update (column );
@@ -320,7 +322,7 @@ private void createRowKeyForMap(ResultSet rs, CacheKey cacheKey, ResultColumnCac
320
322
if (value != null ) {
321
323
cacheKey .update (columnName );
322
324
cacheKey .update (value );
323
- }
325
+ }
324
326
}
325
327
}
326
328
0 commit comments