Skip to content
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

Discriminators don't seem to work for JdbcTemplateMapperFactory using a ResultSetExtractor #748

Open
FrankZappaterrini opened this issue Oct 22, 2020 · 0 comments

Comments

@FrankZappaterrini
Copy link

I am using version 8.2.3, and I cannot add a discriminator column to JdbcTemplateMapperFactory.
Here is my code using JdbcTemplateMapperFactory:

return this.jdbcTemplate.query(this.sqlQueryManager.getMediaItemsQuery(), new MapSqlParameterSource("accountId", accountId), 
					JdbcTemplateMapperFactory.newInstance()
							.addKeys("mediaItemId")
							.discriminator(MediaItem.class)
							.onColumn("uploadType", String.class)
							.with(builder -> builder
									.when("message", Message.class)
									.when(v -> !"message".equals(v), MediaItem.class) 
									)
					.newResultSetExtractor(MediaItem.class));

I expect this code to return a List where some items in the list are the type Message when the value of the "uploadType" column is equal to "message" (Message extends MediaItem). This is not the case. All items in the list are MediaItems. Through debugging, I found the problem. The problem starts in JdbcTemplateMapperFactory.newResultSetExtractor(Type target):

public <T> ResultSetExtractorImpl<T> newResultSetExtractor(Type target) throws MapperBuildingException {
		return new ResultSetExtractorImpl<T>(this.<T>newJdbcMapper(target));
}

The call to JdbcTemplateMapperFactory.newJdbcMapper() switches types to a JdbcMapperFactory:

public <T> JdbcMapper<T> newJdbcMapper(Type target) {
		return JdbcMapperFactory.newInstance(this).newMapper(target);
}

Calling JdbcMapperFactory.newInstance(this) should copy all of the properties from the JdbcTemplateMapperFactory to the new JdbcMapperFactory, but it does not. The copy constructor for the parent class, AbstractMapperFactory, does not copy the list of discriminators:

public AbstractMapperFactory(AbstractMapperFactory<K, ?, S> config) {
		this.fieldMapperErrorHandler = config.fieldMapperErrorHandler;
		this.mapperBuilderErrorHandler = config.mapperBuilderErrorHandler;
		this.consumerErrorHandler = config.consumerErrorHandler;

		this.columnDefinitions = config.columnDefinitions;
		this.identity = config.identity;
		
		this.useAsm = config.useAsm;
		this.failOnAsm = config.failOnAsm;
		this.asmMapperNbFieldsLimit = config.asmMapperNbFieldsLimit;
		
		this.propertyNameMatcherFactory = config.propertyNameMatcherFactory;
		
		this.reflectionService = config.reflectionService;
		this.maxMethodSize = config.maxMethodSize;
		this.assumeInjectionModifiesValues = config.assumeInjectionModifiesValues;
		this.rowFilter = config.rowFilter;
		this.unorderedJoin = config.unorderedJoin;
		this.getterFactory = config.getterFactory;
	}

Therefore, you can never use discriminators with the JdbcTemplateMapperFactory using a ResultSetExtractor. Adding the line:

this.discriminators.addAll(config.discriminators);

to the copy constructor fixes the problem and my code works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant