SaFi Bank Space : SaFi Solve DataAccessException after upgraded Micronaut version of account-manager to 3.6.3

Issue

When trying to upgrade micronuat version to 3.6.3, intergationTest was failed, and the error message is list below.

Error reading object for name [accountId] from result set: The column name accountId was not found in this ResultSet.
io.micronaut.data.exceptions.DataAccessException: Error reading object for name [accountId] from result set: The column name accountId was not found in this ResultSet.
	at app//io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader.exceptionForColumn(ColumnNameResultSetReader.java:248)
	at app//io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader.getRequiredValue(ColumnNameResultSetReader.java:243)
	at app//io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader.getRequiredValue(ColumnNameResultSetReader.java:41)

After compare with previous version of micronaut-data-jdbc 3.3.0 which doesn’t throw this exception, here is the explaination.

Explaination

This method is going to query database from repository layer and return DTO model directly which Micronaut also supported (see https://micronaut-projects.github.io/micronaut-data/3.7.3/guide/index.html#dto ). But the behaviour has chaged.

Version Relations

Previous version

After Upgrade

micronaut-bom

3.4.0

3.7.0

micronaut-data-jdbc

3.3.0

3.7.3

What Micronaut data 3.3.0 do

Inside DefaultJdbcRepositoryOperations, Micronaut trying to get persistent entity of return type InterestHistoryAccrualResult which doesn’t actually entity.

The getEntity method will trying to get registed Entity, and registed entity means eitity class annotationed by @MappedEntity. When this type isn’t exist as entity, will create an entity for this type and register it as

entity.

After doing that, the mapping processor will process result mapping as entity with default naming strategy NamingStrategies.UnderScoreSeparatedLowerCase.

By doing above step, Micronaut data will correctly map result set field from under score style to DTO field with camel style.

What Micronaut data 3.7.3 do

Micronaut updated the mechanism of getting persistent entity.

Now the persistent entity is returned by preparedQuery, which generated by repository interface and the persistent entity is InterestHistoryEntity.

And because of perstent entity is not what we want, persistent property will be null, the mapper processor will map field name with argument.getName(). There isn’t any naming strategey occur, so it’s will query result set with raw field name of dto which is camel style. Then exception threw!

How To Solving This Exception

Regard to previous version’s we should treat our DTO as entity module, means we could simply add @MappedEntity to our dto class definition.

One more thing, currently the error maker InterestHistoryAccrualResult is defined as service layer model, for best practice (see Data classes ), it’s better to define a entity layer model like InterestHistoryAccrualResultEntity and add this @MappedEntity annotation on it, then convert it to original service layer model InterestHistoryAccrualResult.