Home » Java » java – How to run a (insert) stored procedure without a entity class?-Exceptionshub

java – How to run a (insert) stored procedure without a entity class?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am writing some code for my Spring Boot application and I am trying to incorporate a stored procedure. However, I am trying to find a way to run a insert Stored Procedure without needing an entity class. I have tried some ways before but I keep getting errors like java.lang.IllegalArgumentException: No @NamedStoredProcedureQuery was found with that name : InsertGPIP but I know it is because there is no entity associated with it.

Java:

    public void insertToGPIP(){

            StoredProcedureQuery query = entityManager.createNamedStoredProcedureQuery("InsertGPIP")
            .registerStoredProcedureParameter("appcode", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("guid", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("name", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("statuscode", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("assist", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("adid", String.class, ParameterMode.IN)
            .setParameter("appcode", "Rob")
            .setParameter("guid", "F38FJ47")
            .setParameter("name", "A Test")
            .setParameter("statuscode", "02")
            .setParameter("assist", "N")
            .setParameter("adid", "MyId");
            query.execute();

        }
How to&Answers:

From the NamedStoreProcedureQuery javadoc (bold is mine):

The NamedStoredProcedureQuery annotation can be applied to an entity
or mapped superclass
.

A mapped superclass has no table defined for it, only provides mapping information to be inherited by enties that extend it.

So you could have an empty abstract mapped superclass (@MappedSuperclass) as a container for the NamedStoredProcedureQuery annotations.