I am using EntityManager for database operations. I want to execute stored procedure using this EntityManager. I am using below code to execute the procedure but don’t know how to register for In/Out parameters.
Query query = appsEntityManager.createNativeQuery("{call test(?,?,?)}");
query.setParameter(1, "");
query.setParameter(2, "");
query.setParameter(3, "");
query.getResultList();
Please help to solve this.
Is theren’t any way to achieve this problem?
Try this implementations:
Obtain a
java.sql.Connectionusing yourEntityManager:With this
Connectionyou can use thejava.sql.CallableStatementclass to make calls to stored procedures and functions, by this way:Another implementation is based on this post.
Create a class that extends
StoredProcedure:Then, you can invoke your function or stored procedure:
To instantiate
GenericDatabaseCallerwe add some lines to ourapplication-context.xmlNote that the data source is the same that we use to instantiate the
EntityManager.Then in our class we use the annotation
@Respositoryand add the@Autowiredannotation to theGenericDatabaseCallerfield.I hope this works for you.