title: How can i call stored procedure using spring with jpa
body: <p>Am new to SPRING with JPA techniques.</p>
  
  <p>am trying to call the stored procedure which is written in mysql 5. when i am trying to get the data using stored procedure call it spewing exception.</p>
  
  <blockquote>
    <p><code>org.springframework.dao.InvalidDataAccessApiUsageException</code>: <code>org.hibernate.QueryException</code>: query must begin with <code>SELECT</code> or <code>FROM</code>: <code>call [call st_proc_getusers()];</code> nested exception is <code>java.lang.IllegalArgumentException</code>: <code>org.hibernate.QueryException</code>: query must begin with <code>SELECT</code> or <code>FROM</code>: <code>call [call st_proc_getusers()</code>]</p>
  </blockquote>
  
  <p>my <code>persistence.xml</code> is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit  name="SPT3" transaction-type="RESOURCE_LOCAL"&gt;
              &lt;mapping-file&gt;META-INF/persistence-query.xml&lt;/mapping-file&gt;
              &lt;class&gt;com.spt3.pojo.Users&lt;/class&gt;
              &lt;properties&gt;
                      &lt;property name="hibernate.dialect" value="&gt;org.hibernate.dialect.MySQLDialect" /&gt;
                      &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/spring_security" /&gt;
                      &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /&gt;
                      &lt;property name="hibernate.connection.username" value="user" /&gt;
                      &lt;property name="hibernate.connection.password" value="pass" /&gt;
                      &lt;property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/&gt;
                      &lt;property name="hibernate.max_fetch_depth" value="3"/&gt;
                      &lt;property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/&gt;
                      &lt;property name="hibernate.query.substitutions" value="true 1, false 0"/&gt;
                      &lt;property name="hibernate.show_sql" value="true"/&gt;
                      &lt;property name="hibernate.hbm2ddl.auto" value="create"/&gt;
              &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>i tried code is </p>
  
  <pre><code>package com.spt3.dao;
  
  import com.spt3.pojo.Users;
  import java.util.ArrayList;
  import java.util.List;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceException;
  import org.springframework.orm.jpa.JpaCallback;
  import org.springframework.orm.jpa.support.JpaDaoSupport;
  import org.springframework.transaction.annotation.Transactional;
  
  
  @Transactional
  public class JpaUsersDao extends JpaDaoSupport {
  
      public void getResultsByStoredProcedure() {       
          List list=(ArrayList)getJpaTemplate().execute(new JpaCallback() {
              public List doInJpa(EntityManager em) throws PersistenceException {
                  javax.persistence.Query query=em.createQuery("call st_proc_getusers()"); // Here the procedure call 
                  return query.getResultList(); // returning result list
              }
          });        
      }
  
  }
  </code></pre>
  
  <p>Actually i don't know how to call the stored procedure using jpa template.</p>
  
  <p>How can i call stored procedure from spring JPA?</p>
  
  <p>please give the solution to get out from this issue.</p>