micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

Confusing error message if using @Repository in a JDBC project

Open mikehearn opened this issue 11 months ago • 2 comments

Expected Behavior

The error should say something like:

Cannot use a repository annotated with @Repository in a JDBC project, please use @JdbcRepository(dialect = ...) instead.

Actual Behaviour

The Micronaut Data docs are a bit confusing w.r.t. how to annotate repository interfaces. The docs start by saying (under "shared concepts") that you have to annotate them with @Repository which makes sense, but in fact if you use JDBC directly (no JPA/Hibernate) the repository must be annotated with @JdbcRepository. This is explained later but it's easy to miss and if you do, you get an exception with a message like this:

No backing RepositoryOperations configured for repository. Check your configuration and try again

This is pretty confusing. The problem isn't in the app config file, it's not clear what a "backing RepositoryOperations" is or why none is configured, and the generic "check your configuration and try again" advice is so vague it's nearly non-actionable.

Steps To Reproduce

  1. Set up a JDBC only Micronaut Data project.
  2. Write a repository that inherits from e.g. GenericRepository and is annotated using @Repository
  3. Run the app.
  4. Be puzzled by the resulting message.

Environment Information

No response

Example Application

No response

Version

4.4.4

mikehearn avatar Jan 15 '25 11:01 mikehearn

Current error is:

Failed to inject value for parameter [jdbcOperations] of class: io.micronaut.data.jdbc.h2.H2PersonRepository$Intercepted

Message: No bean of type [io.micronaut.data.jdbc.runtime.JdbcOperations] exists. 
* [JdbcOperations] requires the presence of a bean of type [javax.sql.DataSource].
 * [DataSource] requires the presence of a bean of type [io.micronaut.configuration.jdbc.tomcat.DatasourceConfiguration].
  * [DatasourceConfiguration] is disabled because:
   - Configuration requires entries under the prefix: [datasources.default]

What is displayed for you?

dstepanov avatar Mar 05 '25 08:03 dstepanov

I think the error message I see requires a data source to be configured.

The error you see would IMHO be easier to read with an alternate layout that puts the action item first, maybe like this:

Configuration requires entries under the prefix: datasources.default
therefore
... DatasourceConfiguration is disabled because:
... DataSource requires the presence of a bean of type io.micronaut.configuration.jdbc.tomcat.DatasourceConfiguration
... JdbcOperations requires the presence of a bean of type javax.sql.DataSource
... No bean of type io.micronaut.data.jdbc.runtime.JdbcOperations exists.
... Failed to inject value for parameter jdbcOperations of class: io.micronaut.data.jdbc.h2.H2PersonRepository$Intercepted

This layout is probably less intimidating especially to new devs because the thing they have to do comes up front, and chain of implications (which doesn't really matter in this case) comes afterwards where they are less likely to read.

mikehearn avatar Mar 05 '25 09:03 mikehearn