|
| 1 | +package org.springframework.data.jdbc.repository.query; |
| 2 | + |
| 3 | +import org.springframework.context.ApplicationEventPublisher; |
| 4 | +import org.springframework.data.jdbc.core.convert.EntityRowMapper; |
| 5 | +import org.springframework.data.jdbc.core.convert.JdbcConverter; |
| 6 | +import org.springframework.data.jdbc.repository.QueryMappingConfiguration; |
| 7 | +import org.springframework.data.mapping.callback.EntityCallbacks; |
| 8 | +import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
| 9 | +import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; |
| 10 | +import org.springframework.jdbc.core.ResultSetExtractor; |
| 11 | +import org.springframework.jdbc.core.RowMapper; |
| 12 | +import org.springframework.jdbc.core.SingleColumnRowMapper; |
| 13 | + |
| 14 | +/** |
| 15 | + * Default implementation of {@link RowMapperFactory}. Honors the custom mappings defined |
| 16 | + * in {@link QueryMappingConfiguration}. |
| 17 | + * <p> |
| 18 | + * This implementation is not capable of loading the {@link RowMapper} or {@link ResultSetExtractor} |
| 19 | + * by reference via corresponding methods from {@link RowMapperFactory}. |
| 20 | + * |
| 21 | + * @implNote Public APIs of this class are thread-safe. |
| 22 | + * @author Mikhail Polivakha |
| 23 | + */ |
| 24 | +public class DefaultRowMapperFactory implements RowMapperFactory { |
| 25 | + |
| 26 | + private final RelationalMappingContext context; |
| 27 | + private final JdbcConverter converter; |
| 28 | + private final QueryMappingConfiguration queryMappingConfiguration; |
| 29 | + private final EntityCallbacks entityCallbacks; |
| 30 | + private final ApplicationEventPublisher publisher; |
| 31 | + |
| 32 | + public DefaultRowMapperFactory( |
| 33 | + RelationalMappingContext context, |
| 34 | + JdbcConverter converter, |
| 35 | + QueryMappingConfiguration queryMappingConfiguration, |
| 36 | + EntityCallbacks entityCallbacks, |
| 37 | + ApplicationEventPublisher publisher |
| 38 | + ) { |
| 39 | + this.context = context; |
| 40 | + this.converter = converter; |
| 41 | + this.queryMappingConfiguration = queryMappingConfiguration; |
| 42 | + this.entityCallbacks = entityCallbacks; |
| 43 | + this.publisher = publisher; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + @SuppressWarnings("unchecked") |
| 48 | + public RowMapper<Object> getRowMapper(Class<?> returnedObjectType) { |
| 49 | + |
| 50 | + RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType); |
| 51 | + |
| 52 | + if (persistentEntity == null) { |
| 53 | + return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType, |
| 54 | + converter.getConversionService()); |
| 55 | + } |
| 56 | + |
| 57 | + return (RowMapper<Object>) determineDefaultMapper(returnedObjectType); |
| 58 | + } |
| 59 | + |
| 60 | + private RowMapper<?> determineDefaultMapper(Class<?> returnedObjectType) { |
| 61 | + |
| 62 | + RowMapper<?> configuredQueryMapper = queryMappingConfiguration.getRowMapper(returnedObjectType); |
| 63 | + |
| 64 | + if (configuredQueryMapper != null) { |
| 65 | + return configuredQueryMapper; |
| 66 | + } |
| 67 | + |
| 68 | + EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( // |
| 69 | + context.getRequiredPersistentEntity(returnedObjectType), // |
| 70 | + converter // |
| 71 | + ); |
| 72 | + |
| 73 | + return new CallbackCapableRowMapper<>(defaultEntityRowMapper, publisher, entityCallbacks); |
| 74 | + } |
| 75 | +} |
0 commit comments