@Test public void testMappedQuery() throws Exception { Field field = Foo.class.getDeclaredField(Foo.ID_COLUMN_NAME); String tableName = "basefoo"; FieldType[] resultFieldTypes = new FieldType[] { FieldType.createFieldType(connectionSource, tableName, field, Foo.class) }; BaseMappedQuery<Foo, Integer> baseMappedQuery = new BaseMappedQuery<Foo, Integer>(baseFooTableInfo, "select * from " + tableName, new FieldType[0], resultFieldTypes) { }; DatabaseResults results = createMock(DatabaseResults.class); int colN = 1; expect(results.getObjectCacheForRetrieve()).andReturn(null); expect(results.getObjectCacheForStore()).andReturn(null); expect(results.findColumn(Foo.ID_COLUMN_NAME)).andReturn(colN); int id = 63365; expect(results.getInt(colN)).andReturn(id); replay(results); Foo baseFoo = baseMappedQuery.mapRow(results); assertNotNull(baseFoo); assertEquals(id, baseFoo.id); verify(results); }
@Override public T mapRow(DatabaseResults results) throws SQLException { Map<String, Integer> colPosMap; if (columnPositions == null) { colPosMap = new HashMap<String, Integer>(); } else { colPosMap = columnPositions; } ObjectCache objectCache = results.getObjectCacheForRetrieve(); if (objectCache != null) { Object id = idField.resultToJava(results, colPosMap); T cachedInstance = objectCache.get(clazz, id); if (cachedInstance != null) { return cachedInstance; } } T instance = tableInfo.createObject(); Object id = null; boolean foreignCollections = false; for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { foreignCollections = true; } else { Object val = fieldType.resultToJava(results, colPosMap); if (val != null && parent != null && fieldType.getField().getType() == parent.getClass() && val.equals(parentId)) { fieldType.assignField(instance, parent, true, objectCache); } else { fieldType.assignField(instance, val, false, objectCache); } if (fieldType.isId()) { id = val; } } } if (foreignCollections) { for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { BaseForeignCollection<?, ?> collection = fieldType.buildForeignCollection(instance, id); if (collection != null) { fieldType.assignField(instance, collection, false, objectCache); } } } } objectCache = results.getObjectCacheForStore(); if (objectCache != null && id != null) { objectCache.put(clazz, id, instance); } if (columnPositions == null) { columnPositions = colPosMap; } return instance; }
BaseMappedQuery extends BaseMappedStatement<T, ID> implements GenericRowMapper<T> { @Override public T mapRow(DatabaseResults results) throws SQLException { Map<String, Integer> colPosMap; if (columnPositions == null) { colPosMap = new HashMap<String, Integer>(); } else { colPosMap = columnPositions; } ObjectCache objectCache = results.getObjectCacheForRetrieve(); if (objectCache != null) { Object id = idField.resultToJava(results, colPosMap); T cachedInstance = objectCache.get(clazz, id); if (cachedInstance != null) { return cachedInstance; } } T instance = tableInfo.createObject(); Object id = null; boolean foreignCollections = false; for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { foreignCollections = true; } else { Object val = fieldType.resultToJava(results, colPosMap); if (val != null && parent != null && fieldType.getField().getType() == parent.getClass() && val.equals(parentId)) { fieldType.assignField(instance, parent, true, objectCache); } else { fieldType.assignField(instance, val, false, objectCache); } if (fieldType.isId()) { id = val; } } } if (foreignCollections) { for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { BaseForeignCollection<?, ?> collection = fieldType.buildForeignCollection(instance, id); if (collection != null) { fieldType.assignField(instance, collection, false, objectCache); } } } } objectCache = results.getObjectCacheForStore(); if (objectCache != null && id != null) { objectCache.put(clazz, id, instance); } if (columnPositions == null) { columnPositions = colPosMap; } return instance; } }
BaseMappedQuery extends BaseMappedStatement<T, ID> implements GenericRowMapper<T> { @Override public T mapRow(DatabaseResults results) throws SQLException { Map<String, Integer> colPosMap; if (columnPositions == null) { colPosMap = new HashMap<String, Integer>(); } else { colPosMap = columnPositions; } ObjectCache objectCache = results.getObjectCacheForRetrieve(); if (objectCache != null) { Object id = idField.resultToJava(results, colPosMap); T cachedInstance = objectCache.get(clazz, id); if (cachedInstance != null) { return cachedInstance; } } T instance = tableInfo.createObject(); Object id = null; boolean foreignCollections = false; for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { foreignCollections = true; } else { Object val = fieldType.resultToJava(results, colPosMap); if (val != null && parent != null && fieldType.getField().getType() == parent.getClass() && val.equals(parentId)) { fieldType.assignField(instance, parent, true, objectCache); } else { fieldType.assignField(instance, val, false, objectCache); } if (fieldType.isId()) { id = val; } } } if (foreignCollections) { for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { BaseForeignCollection<?, ?> collection = fieldType.buildForeignCollection(instance, id); if (collection != null) { fieldType.assignField(instance, collection, false, objectCache); } } } } objectCache = results.getObjectCacheForStore(); if (objectCache != null && id != null) { objectCache.put(clazz, id, instance); } if (columnPositions == null) { columnPositions = colPosMap; } return instance; } protected  BaseMappedQuery(TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes,
			FieldType[] resultsFieldTypes); }
BaseMappedQuery extends BaseMappedStatement<T, ID> implements GenericRowMapper<T> { @Override public T mapRow(DatabaseResults results) throws SQLException { Map<String, Integer> colPosMap; if (columnPositions == null) { colPosMap = new HashMap<String, Integer>(); } else { colPosMap = columnPositions; } ObjectCache objectCache = results.getObjectCacheForRetrieve(); if (objectCache != null) { Object id = idField.resultToJava(results, colPosMap); T cachedInstance = objectCache.get(clazz, id); if (cachedInstance != null) { return cachedInstance; } } T instance = tableInfo.createObject(); Object id = null; boolean foreignCollections = false; for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { foreignCollections = true; } else { Object val = fieldType.resultToJava(results, colPosMap); if (val != null && parent != null && fieldType.getField().getType() == parent.getClass() && val.equals(parentId)) { fieldType.assignField(instance, parent, true, objectCache); } else { fieldType.assignField(instance, val, false, objectCache); } if (fieldType.isId()) { id = val; } } } if (foreignCollections) { for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { BaseForeignCollection<?, ?> collection = fieldType.buildForeignCollection(instance, id); if (collection != null) { fieldType.assignField(instance, collection, false, objectCache); } } } } objectCache = results.getObjectCacheForStore(); if (objectCache != null && id != null) { objectCache.put(clazz, id, instance); } if (columnPositions == null) { columnPositions = colPosMap; } return instance; } protected  BaseMappedQuery(TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes,
			FieldType[] resultsFieldTypes); @Override T mapRow(DatabaseResults results); void setParentInformation(Object parent, Object parentId); }
BaseMappedQuery extends BaseMappedStatement<T, ID> implements GenericRowMapper<T> { @Override public T mapRow(DatabaseResults results) throws SQLException { Map<String, Integer> colPosMap; if (columnPositions == null) { colPosMap = new HashMap<String, Integer>(); } else { colPosMap = columnPositions; } ObjectCache objectCache = results.getObjectCacheForRetrieve(); if (objectCache != null) { Object id = idField.resultToJava(results, colPosMap); T cachedInstance = objectCache.get(clazz, id); if (cachedInstance != null) { return cachedInstance; } } T instance = tableInfo.createObject(); Object id = null; boolean foreignCollections = false; for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { foreignCollections = true; } else { Object val = fieldType.resultToJava(results, colPosMap); if (val != null && parent != null && fieldType.getField().getType() == parent.getClass() && val.equals(parentId)) { fieldType.assignField(instance, parent, true, objectCache); } else { fieldType.assignField(instance, val, false, objectCache); } if (fieldType.isId()) { id = val; } } } if (foreignCollections) { for (FieldType fieldType : resultsFieldTypes) { if (fieldType.isForeignCollection()) { BaseForeignCollection<?, ?> collection = fieldType.buildForeignCollection(instance, id); if (collection != null) { fieldType.assignField(instance, collection, false, objectCache); } } } } objectCache = results.getObjectCacheForStore(); if (objectCache != null && id != null) { objectCache.put(clazz, id, instance); } if (columnPositions == null) { columnPositions = colPosMap; } return instance; } protected  BaseMappedQuery(TableInfo<T, ID> tableInfo, String statement, FieldType[] argFieldTypes,
			FieldType[] resultsFieldTypes); @Override T mapRow(DatabaseResults results); void setParentInformation(Object parent, Object parentId);  }