question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

micronaut-jdbc fails to map when the property has a prefix

See original GitHub issue

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

Use micronaut-jdbc. Annotate a relation and have the property prefixed. We are migrating parts of a legacy system and renaming the actual column name is no (yet) an option.

data class Account (
  // ...
  @MappedProperty("fk_account_id")
  @Relation(value = Relation.Kind.MANY_TO_ONE)
  var account: Account
  // ...
)

@JdbcRepository
abstract class SessionRepository : CrudRepository<Session, Int> {
    @Join(value = "account", type = Join.Type.LEFT_FETCH)
    abstract override fun findById(id: Int): Optional<Session>
}

Expected Behaviour

The MappedProperty value is used to create the mapped association. This seem to happen when the MappedProperty value has no prefixes. The linked GitHub repository has both use cases tested: a relation with a prefixed property and unprefixed.

Actual Behaviour

Micronaut tries to use the variable name to create the object, even if not present in the request. This behavior is not present if the property has no prefix.

Demo stacktrace

19:26:16.403 [main] DEBUG io.micronaut.data.query - Executing Query: SELECT session_a_.`id`,session_a_.`fk_account_id`,session_a_account_.`name` AS account_name FROM `session_a` session_a_ LEFT JOIN account session_a_account_ ON session_a_.fk_account_id=session_a_account_.id WHERE (session_a_.`id` = ?)

io.micronaut.data.exceptions.DataAccessException: Error reading object for name [account_id] from result set: No such column: account_id
	at io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader.exceptionForColumn(ColumnNameResultSetReader.java:206)
	at io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader.readInt(ColumnNameResultSetReader.java:112)
	at io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader.readInt(ColumnNameResultSetReader.java:35)
	at io.micronaut.data.runtime.mapper.ResultReader.readDynamic(ResultReader.java:110)
	at io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper.readEntity(SqlResultEntityTypeMapper.java:274)
	at io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper.readAssociation(SqlResultEntityTypeMapper.java:503)
	at io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper.readEntity(SqlResultEntityTypeMapper.java:255)
	at io.micronaut.data.runtime.mapper.sql.SqlResultEntityTypeMapper.map(SqlResultEntityTypeMapper.java:146)
	at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.lambda$findOne$2(DefaultJdbcRepositoryOperations.java:193)
	at io.micronaut.transaction.support.AbstractSynchronousTransactionManager.executeRead(AbstractSynchronousTransactionManager.java:157)
	at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.findOne(DefaultJdbcRepositoryOperations.java:177)
	at io.micronaut.data.runtime.intercept.DefaultFindOptionalInterceptor.intercept(DefaultFindOptionalInterceptor.java:47)
	at io.micronaut.data.runtime.intercept.DefaultFindOptionalInterceptor.intercept(DefaultFindOptionalInterceptor.java:34)
	at io.micronaut.data.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:79)
	at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69)
	at optionalrelation.SessionARepository$Intercepted.findById(Unknown Source)
	at optionalrelation.ApplicationTest.findById() should fetch a Session with a prefixed relation column(ApplicationTest.kt:50)
        ...
Caused by: java.sql.SQLSyntaxErrorException: No such column: account_id
	at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:243)
	at org.mariadb.jdbc.internal.com.read.dao.ColumnNameMap.getIndex(ColumnNameMap.java:132)
	at org.mariadb.jdbc.internal.com.read.resultset.SelectResultSet.findColumn(SelectResultSet.java:1314)
	at org.mariadb.jdbc.internal.com.read.resultset.SelectResultSet.getInt(SelectResultSet.java:986)
	at com.zaxxer.hikari.pool.HikariProxyResultSet.getInt(HikariProxyResultSet.java)
	at io.micronaut.data.jdbc.mapper.ColumnNameResultSetReader.readInt(ColumnNameResultSetReader.java:110)
	... 78 more

Environment Information

  • Operating System: Linux
  • Micronaut Version: 1.2.6. micronaut-data:1.0.0.BUILD_SNAPSHOT
  • JDK Version: 8

Example Application

https://github.com/Chris-V/micronautjdbc-relation-tests

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
graemerochercommented, Dec 5, 2019

Thanks, will look into it soon. I am working on some compiler performance optimizations right now and will get back to bug fixing in the next few days

2reactions
hrpathcommented, Jan 10, 2020

Thanks @graemerocher !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Micronaut Data - GitHub Pages
If the projected property type and the return generic type do not match up then Micronaut Data will fail to compile the method....
Read more >
Management & Monitoring - Micronaut Documentation
Micronaut TOML allows you to write your application configuration with TOML in addition to Properties , YAML , Groovy or Config4k . Micronaut...
Read more >
Spring Boot read properties without prefix to a map
properties file in a map In the code below the property test has the respective value but the map is empty. How can...
Read more >
Vendor Prefix - MDN Web Docs Glossary
Vendor Prefix. Browser vendors used to add prefixes to experimental or nonstandard CSS properties and JavaScript APIs, so developers could ...
Read more >
Dealing with Vendor Prefixes in CSS and JavaScript - YouTube
Learn all about what vendor prefixes are, why they exist, and how to make working with them in CSS and JavaScript fun.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found