- 
                Notifications
    You must be signed in to change notification settings 
- Fork 377
Unified RowMapper infrastructure #2000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            54 changes: 54 additions & 0 deletions
          
          54 
        
  ...main/java/org/springframework/data/jdbc/repository/query/AbstractDelegatingRowMapper.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2020-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.data.jdbc.repository.query; | ||
|  | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|  | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.lang.Nullable; | ||
| import org.springframework.util.Assert; | ||
|  | ||
| /** | ||
| * Abstract {@link RowMapper} that delegates the actual mapping logic to a {@link AbstractDelegatingRowMapper#delegate delegate} | ||
| * | ||
| * @author Mikhail Polivakha | ||
| */ | ||
| public abstract class AbstractDelegatingRowMapper<T> implements RowMapper<T> { | ||
|  | ||
| private final RowMapper<T> delegate; | ||
|  | ||
| protected AbstractDelegatingRowMapper(RowMapper<T> delegate) { | ||
| Assert.notNull(delegate, "Delegating RowMapper cannot be null"); | ||
|  | ||
| this.delegate = delegate; | ||
| } | ||
|  | ||
| @Override | ||
| public T mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
| T intermediate = delegate.mapRow(rs, rowNum); | ||
| return postProcessMapping(intermediate); | ||
| } | ||
|  | ||
| /** | ||
| * The post-processing callback for implementations. | ||
| * | ||
| * @return the mapped entity after applying post-processing logic | ||
| */ | ||
| protected T postProcessMapping(@Nullable T object) { | ||
| return object; | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            59 changes: 59 additions & 0 deletions
          
          59 
        
  ...rc/main/java/org/springframework/data/jdbc/repository/query/CallbackCapableRowMapper.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright 2020-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.data.jdbc.repository.query; | ||
|  | ||
| import java.sql.ResultSet; | ||
|  | ||
| import org.springframework.context.ApplicationEventPublisher; | ||
| import org.springframework.data.mapping.callback.EntityCallbacks; | ||
| import org.springframework.data.relational.core.mapping.event.AfterConvertCallback; | ||
| import org.springframework.data.relational.core.mapping.event.AfterConvertEvent; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.lang.Nullable; | ||
|  | ||
| /** | ||
| * Delegating {@link RowMapper} implementation that applies post-processing logic | ||
| * after the {@link RowMapper#mapRow(ResultSet, int)}. In particular, it emits the | ||
| * {@link AfterConvertEvent} event and invokes the {@link AfterConvertCallback} callbacks. | ||
| * | ||
| * @author Mark Paluch | ||
| * @author Mikhail Polivakha | ||
| */ | ||
| public class CallbackCapableRowMapper<T> extends AbstractDelegatingRowMapper<T> { | ||
|  | ||
| private final ApplicationEventPublisher publisher; | ||
| private final @Nullable EntityCallbacks callbacks; | ||
|  | ||
| public CallbackCapableRowMapper(RowMapper<T> delegate, ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks) { | ||
| super(delegate); | ||
| this.publisher = publisher; | ||
| this.callbacks = callbacks; | ||
| } | ||
|  | ||
| @Override | ||
| public T postProcessMapping(@Nullable T object) { | ||
| if (object != null) { | ||
|  | ||
| publisher.publishEvent(new AfterConvertEvent<>(object)); | ||
|  | ||
| if (callbacks != null) { | ||
| return callbacks.callback(AfterConvertCallback.class, object); | ||
| } | ||
|  | ||
| } | ||
| return object; | ||
| } | ||
| } | 
        
          
          
            43 changes: 43 additions & 0 deletions
          
          43 
        
  ...dbc/src/main/java/org/springframework/data/jdbc/repository/query/ConvertingRowMapper.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2020-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.data.jdbc.repository.query; | ||
|  | ||
| import org.springframework.core.convert.converter.Converter; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.lang.Nullable; | ||
|  | ||
| /** | ||
| * Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterwards into {@code Object}. | ||
| * | ||
| * @author Mark Paluch | ||
| * @author Mikhail Polivakha | ||
| * | ||
| * @since 2.3 | ||
| */ | ||
| public class ConvertingRowMapper extends AbstractDelegatingRowMapper<Object> { | ||
|  | ||
| private final Converter<Object, Object> converter; | ||
|  | ||
| public ConvertingRowMapper(RowMapper<Object> delegate, Converter<Object, Object> converter) { | ||
| super(delegate); | ||
| this.converter = converter; | ||
| } | ||
|  | ||
| @Override | ||
| public Object postProcessMapping(@Nullable Object object) { | ||
| return object != null ? converter.convert(object) : null; | ||
| } | ||
| } | 
        
          
          
            90 changes: 90 additions & 0 deletions
          
          90 
        
  ...src/main/java/org/springframework/data/jdbc/repository/query/DefaultRowMapperFactory.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright 2020-2025 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.data.jdbc.repository.query; | ||
|  | ||
| import org.springframework.context.ApplicationEventPublisher; | ||
| import org.springframework.data.jdbc.core.convert.EntityRowMapper; | ||
| import org.springframework.data.jdbc.core.convert.JdbcConverter; | ||
| import org.springframework.data.jdbc.repository.QueryMappingConfiguration; | ||
| import org.springframework.data.mapping.callback.EntityCallbacks; | ||
| import org.springframework.data.relational.core.mapping.RelationalMappingContext; | ||
| import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; | ||
| import org.springframework.jdbc.core.ResultSetExtractor; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.jdbc.core.SingleColumnRowMapper; | ||
|  | ||
| /** | ||
| * Default implementation of {@link RowMapperFactory}. Honors the custom mappings defined | ||
| * in {@link QueryMappingConfiguration}. | ||
| * <p> | ||
| * This implementation is not capable of loading the {@link RowMapper} or {@link ResultSetExtractor} | ||
| * by reference via corresponding methods from {@link RowMapperFactory}. | ||
| * | ||
| * @implNote Public APIs of this class are thread-safe. | ||
| * @author Mikhail Polivakha | ||
| */ | ||
| public class DefaultRowMapperFactory implements RowMapperFactory { | ||
|  | ||
| private final RelationalMappingContext context; | ||
| private final JdbcConverter converter; | ||
| private final QueryMappingConfiguration queryMappingConfiguration; | ||
| private final EntityCallbacks entityCallbacks; | ||
| private final ApplicationEventPublisher publisher; | ||
|  | ||
| public DefaultRowMapperFactory( | ||
| RelationalMappingContext context, | ||
| JdbcConverter converter, | ||
| QueryMappingConfiguration queryMappingConfiguration, | ||
| EntityCallbacks entityCallbacks, | ||
| ApplicationEventPublisher publisher | ||
| ) { | ||
| this.context = context; | ||
| this.converter = converter; | ||
| this.queryMappingConfiguration = queryMappingConfiguration; | ||
| this.entityCallbacks = entityCallbacks; | ||
| this.publisher = publisher; | ||
| } | ||
|  | ||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public RowMapper<Object> create(Class<?> returnedObjectType) { | ||
|  | ||
| RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType); | ||
|  | ||
| if (persistentEntity == null) { | ||
| return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType, | ||
| converter.getConversionService()); | ||
| } | ||
|  | ||
| return (RowMapper<Object>) determineDefaultMapper(returnedObjectType); | ||
| } | ||
|  | ||
| private RowMapper<?> determineDefaultMapper(Class<?> returnedObjectType) { | ||
|  | ||
| RowMapper<?> configuredQueryMapper = queryMappingConfiguration.getRowMapper(returnedObjectType); | ||
|  | ||
| if (configuredQueryMapper != null) { | ||
| return configuredQueryMapper; | ||
| } | ||
|  | ||
| EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( // | ||
| context.getRequiredPersistentEntity(returnedObjectType), // | ||
| converter // | ||
| ); | ||
|  | ||
| return new CallbackCapableRowMapper<>(defaultEntityRowMapper, publisher, entityCallbacks); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.