Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling stored procedure from Java / JPA
    primarykey
    data
    text
    <p>I am writing a simple web application to call a stored procedure and retrieve some data. Its a very simple application, which interacts with client's database. We pass employee id and company id and the stored procedure will return employee details.</p> <p>Web application cannot update/delete data and is using SQL Server.</p> <p>I am deploying my web application in Jboss AS. Should I use JPA to access the stored procedure or <code>CallableStatement</code>. Any advantage of using JPA in this case.</p> <p>Also what will be the sql statement to call this stored procedure. I have never used stored procedures before and I am struggling with this one. Google was not much of a help.</p> <p>Here is the stored procedure:</p> <pre><code>CREATE procedure getEmployeeDetails (@employeeId int, @companyId int) as begin select firstName, lastName, gender, address from employee et where et.employeeId = @employeeId and et.companyId = @companyId end </code></pre> <p>Update:</p> <p>For anyone else having problem calling stored procedure using <em>JPA</em>.</p> <pre><code>Query query = em.createNativeQuery("{call getEmployeeDetails(?,?)}", EmployeeDetails.class) .setParameter(1, employeeId) .setParameter(2, companyId); List&lt;EmployeeDetails&gt; result = query.getResultList(); </code></pre> <p>Things I have noticed:</p> <ol> <li>Parameter names didn't work for me, so try using parameter index.</li> <li>Correct sql statement <code>{call sp_name(?,?)}</code> instead of <code>call sp_name(?,?)</code></li> <li>If stored procedure is returning a result set, even if you know with only one row, <code>getSingleResult</code> wont work</li> <li>Pass a <code>resultSetMapping</code> name or result class details</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload