Reputation: 1969
I am developing an application that uses JPA 2.0 with MySQL 5 database.
I know that JPA hides the calls to the DB queries and makes it underthehood, and all i use is JPQL,
now does this makes the need for stored procedures less ? and how can i use stored procedures with JPA 2.0 ?
Upvotes: 1
Views: 4019
Reputation: 9935
if you use EclipseLink
JPA, you can use @NamedStoredProcedureQuery
annotation for your stored procedures.
Example :
@NamedStoredProcedureQuery(
name="findAllEmployees",
procedureName="EMP_READ_ALL",
resultClass=Employee.class,
parameters = {
@StoredProcedureParameter(
queryParameter="result",
name="RESULT_CURSOR",
direction=Direction.OUT_CURSOR
)
}
)
@Entity
public class Employee {
...
}
You can reference here.
Upvotes: 1
Reputation: 1727
Stored procedures have mostly gone out of favor (you can read some ideas on why here: http://www.codinghorror.com/blog/2004/10/who-needs-stored-procedures-anyways.html).
In general unless you have performance issues or are working with legacy systems, it's advised not to use stored procedures. Object Relational Mapping solutions, such as JPA, are now the industry standard, mostly because they let you write less, and are easier to maintain then other methods (such as using the DAO pattern and JDBC directly).
For example consider adding a new column to a table you are using CRUD (Create,retrieve,update,delete) with:
In regards to using stored procedures, JPA doesn't have support for stored procedures per se, but there are two way you can work around it:
You can use the "native query" feature, that let's you write an sql in the Native database query language, for instance for mySQL you can write something like this:
Query query = entityManager.createNativeQuery("{ CALL procedure(?) }");
query.setParameter(1, parameterName);
You need to also remember that the procedure must return a result set and you can't use out parameters.
Upvotes: 3