1

I have stored procedures that need to be called to perform the operation. However, for each operation, I have saved the stored procedure name into the database. So, at runtime, based on the operation name I will call the assign stored procedure.

Question: 1. How can I set the name of the NamedStoredProcedureQuery at runtime?

I am using Spring JPA with Spring boot.

@NamedStoredProcedureQueries({
        @NamedStoredProcedureQuery(
                name = "sptest",
                procedureName = "usp_helper_test",
                resultClasses = {Config.class},
                parameters = {
                        @StoredProcedureParameter(
                                name = "data",
                                type = String.class,
                                mode = ParameterMode.IN)
                })
})

In this above example, I want to set procedureName at runtime.

Developer
  • 493
  • 2
  • 8
  • 19

1 Answers1

1

If we ignore stuff like byte code generation: You can't. Named stored procedures get their name from the annotations you showed in the question.

Of course you can still use either the EntityManager or JDBC (possibly via the JdbcTemplate) to call stored procedures by the name they have in the database.

With the EntityManager you'd invoke EntityManager.createStoredProcedureQuery in one of it's variants.

For the JdbcTemplate approach you can consult this SO answer.

The code you need to write would go in a custom method implementation.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348