[英]JPA JPQl or Stored Procedures in MySQL
I am developing an application that uses JPA 2.0 with MySQL 5 database. 我正在开发一个使用JPA 2.0和MySQL 5数据库的应用程序。
I know that JPA hides the calls to the DB queries and makes it underthehood, and all i use is JPQL, 我知道JPA隐藏了对数据库查询的调用并使其成为继续,我使用的只是JPQL,
now does this makes the need for stored procedures less ? 现在这是否会减少对存储过程的需求? and how can i use stored procedures with JPA 2.0 ? 以及如何在JPA 2.0中使用存储过程?
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 ). 存储过程大多不受欢迎(您可以在此处阅读有关原因的一些想法: 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). 对象关系映射解决方案(如JPA)现在已成为行业标准,主要是因为它们可以让您编写更少的内容,并且比其他方法(例如直接使用DAO模式和JDBC)更容易维护。
For example consider adding a new column to a table you are using CRUD (Create,retrieve,update,delete) with: 例如,考虑使用CRUD(创建,检索,更新,删除)向表添加新列:
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: 关于使用存储过程,JPA本身不支持存储过程,但有两种方法可以解决它:
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: 您可以使用“本机查询”功能,让您使用Native数据库查询语言编写sql,例如对于mySQL,您可以编写如下内容:
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. 您还需要记住,该过程必须返回结果集,并且您不能使用out参数。
if you use EclipseLink
JPA, you can use @NamedStoredProcedureQuery
annotation for your stored procedures. 如果使用EclipseLink
JPA,则可以对存储过程使用@NamedStoredProcedureQuery
批注。
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 {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.