spring-dataspring-data-jdbc

A custom annotation above the method to generate SQL by the name of the method


I'm trying to make an annotation that will participate in SQL generation on a spring-date-jdbc. You can say this is a hint for using an index in a database. It can look like:

@UseIndex("name_index")
findByName(String name)

I found a way to implement an interceptor from JdbcRepositoryFactoryBean:

    class UseIndexInterceptor : MethodInterceptor {

        override fun invoke(invocation: MethodInvocation): Any? {
            val useIndex = invocation.method.getAnnotation(UseIndex::class.java)
            threadBoundUseIndex.set(useIndex)
            try {
                return invocation.proceed()
            } finally {
                threadBoundUseIndex.remove()
            }
        }
    }

And use it in MySelectRenderContext

And the question is, is it possible to do something better?


Solution

  • Answer on Github issue: https://github.com/spring-projects/spring-data-relational/issues/1864

    Methods invoked on the repository interface are already exposed in a thread-bound arrangement through ExposeInvocationInterceptor. You can obtain the currently invoked method by calling ExposeInvocationInterceptor.currentInvocation().