Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:54:29+00:00 2026-06-06T01:54:29+00:00

I am using EntityManager for database operations. I want to execute stored procedure using

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:54:32+00:00Added an answer on June 6, 2026 at 1:54 am

    Try this implementations:

    Obtain a java.sql.Connection using your EntityManager:

    Connection cc = ((SessionImpl) em.getDelegate()).connection();
    

    With this Connection you can use the java.sql.CallableStatement class to make calls to stored procedures and functions, by this way:

    CallableStatement callableStatement;
    
    try {  
      callableStatement = cc.prepareCall("{call stored_proc(?,?,?,?,?)}");
    
      callableStatement.setString(1, "1");//Parameter #1
      callableStatement.setString(2, "ET");////Parameter #2
      callableStatement.setString(3, "|s|");// //Parameter #3
      callableStatement.registerOutParameter(4, Types.INTEGER); //Output # 1
      callableStatement.registerOutParameter(5, Types.VARCHAR); //Output # 2
      callableStatement.execute();
    
      Integer outputValue = callableStatement.getInt(4);
      String outputValue1 = callableStatement.getString(5);
    }
    

    Another implementation is based on this post.

    Create a class that extends StoredProcedure:

    /**
     * Class to provide access to the database. With this class you can invoke functions and stored procedures.
     */
    public class GenericDatabaseCaller {
    
      /**Data source. */
      private DataSource dataSource;
    
      /**
       * This method requires LinkedHashMaps for inParams and outParams so that parameters can be set in a
       * sequence.
       * @param functionName Name of the stored procedure or function.
       * @param isFunction indicates if the process to execute is a Function or a Stored procedure.
       * @param inParams {@link LinkedHashMap} of IN parameters.
       * @param outParams {@link LinkedHashMap} of OUT Parameters.
       * @return {@link Map} with the output parameters.
       */
      public Map executeSimpleProcedure(String functionName, boolean isFunction, Map<String, Object> inParams,
          Map<String, Object> outParams) {
        InnerStoredProcedure innerStoredProcedure = new InnerStoredProcedure(dataSource, functionName, isFunction,
            inParams, outParams);
        return innerStoredProcedure.executeProcedure(inParams);
      }
    
      private class InnerStoredProcedure extends StoredProcedure {
    
        /**
         * @param ds
         * @param SQL
         * @param isFunction
         * @param inParams
         * @param outParams
         */
        public InnerStoredProcedure(DataSource ds, String SQL, boolean isFunction,  Map<String, Object> inParams, Map<String, Object> outParams) {
          setDataSource(ds);
          setFunction(isFunction);
          setSql(SQL);
          configerParameters(inParams, outParams);
          compile();
        }
    
        /**
         * Configure the input and output parameters for the stored procedure
         * @param inParams
         * @param outputParamers
         */
        public void configerParameters(Map<String, Object> inParams, Map<String, Object> outputParamers) {
          if (inParams != null && inParams.size() > 0) {
            Iterator<String> keySetIterator = inParams.keySet().iterator();
            while (keySetIterator.hasNext()) {
              String key = keySetIterator.next();
              if (inParams.get(key) instanceof String) {
                declareParameter(new SqlParameter(key, Types.VARCHAR));
              } else if (inParams.get(key) instanceof Integer) {
                declareParameter(new SqlParameter(key, Types.INTEGER));
              } else if (inParams.get(key) instanceof Date || inParams.get(key) instanceof java.sql.Date) {
                declareParameter(new SqlParameter(key, Types.DATE));
              }
              // TODO Add more types.
            }
          }
    
          if (outputParamers != null && outputParamers.size() > 0) {
            Iterator<String> keySetIterator = outputParamers.keySet().iterator();
            while (keySetIterator.hasNext()) {
              String key = keySetIterator.next();
              if (outputParamers.get(key) instanceof String) {
                declareParameter(new SqlOutParameter(key, Types.VARCHAR));
              } else if (outputParamers.get(key) instanceof Integer) {
                declareParameter(new SqlOutParameter(key, Types.INTEGER));
              }
            }
          }
        }
    
        public Map executeProcedure(Map inputs) {
    
          return execute(inputs);
        }
      }
    }
    

    Then, you can invoke your function or stored procedure:

    String procedureName = "stored_proc";
    
    Map<String, Object> inMap = new LinkedHashMap<String, Object>();
    inMap.put("parameter1", "10");
    inMap.put("parameter2", "|Lib");
    inMap.put("parameter3", "P");   
    
    Map<String, Object> outMap = new LinkedHashMap<String, Object>();
    outMap.put("output", 0);
    outMap.put("output1", "");
    
    Map resultMap = genericDatabaseCaller.executeSimpleProcedure(procedureName, inMap, outMap);
    

    To instantiate GenericDatabaseCaller we add some lines to our application-context.xml

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    <bean id="genericDatabaseCaller"
        class="co.com.custom.GenericDatabaseCaller">
        <property name="dataSource" ref="dataSource" />
    </bean>
    

    Note that the data source is the same that we use to instantiate the EntityManager.

    Then in our class we use the annotation @Respository and add the @Autowired annotation to the GenericDatabaseCaller field.

    @Repository(value = "customDao")
    public class JPACustomDao implements CustomDao {
    
      /** entity manager. */
      private EntityManager em = null;
    
      /**
       * Sets the entity manager.
       * 
       * @param entityManager {@link EntityManager}.
       */
      @PersistenceContext
      public void setEntityManager(EntityManager entityManager) {
        this.em = entityManager;
      }
    
      @Autowired
      private GenericDatabaseCaller genericStoredProcedure;
    }
    

    I hope this works for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the EntityManager to persist data into my database. public void save(X x){
After constructing the bean, I want to retrieve data from the database, using the
When my code issues a call like this: entityManager.find(Customer.class, customerID); How can I see
I am using Hibernate JPA and Spring with a Mysql database and I want
Im querying a database using JPQL but I cannot retrieve the 'Report' table's rows
Using LINQ on collections, what is the difference between the following lines of code?
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
Using Nunit, I want to be able to write a test fixture that will
Using Flex 3, I would like to take an image snapshot such as this:
Using top it's easy to identify processes that are hogging memory and cpu, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.