简体   繁体   English

JPA JPQl或MySQL中的存储过程

[英]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(创建,检索,更新,删除)向表添加新列:

  • Using a JPA you add the column to the table and add the property to the class with an annotation, that's it. 使用JPA,您可以将列添加到表中,并使用注释将该属性添加到类中,就是这样。
  • With stored procedures, you'll also need to add the parameter to the procedures to update or create a new entity, you'll need to add the column to the insert or update statement inside the procedure, in some cases you'll need to add the column to the select clause inside the procedure. 使用存储过程,您还需要将参数添加到过程以更新或创建新实体,您需要将该列添加到过程内的insert或update语句,在某些情况下您需要将列添加到过程内的select子句。

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本身不支持存储过程,但有两种方法可以解决它:

  • Some JPA implementation (such as EclipseLink or Hibernate) support stored procedures, and you can use their implementation directly , you'll have to see what implementation you are using. 一些JPA实现(例如EclipseLink或Hibernate)支持存储过程,您可以直接使用它们的实现,您将必须查看您正在使用的实现。
  • 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 {
  ...
}

You can reference here . 你可以在这里参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM