`

Call Stored Procedure with JPA 2.1

阅读更多
JPA 2.1 introduces APIs to call Stored Procedure. It has similar programming pattern as executing a common CRUD sql statement.

@NamedStoredProcedureQuery: defines details of the stored proc to call
@StoredProcedureParameter: defines IN/OUT parameters of the stored proc
StoredProcedureQuery: Interface used to control stored procedure query execution

-- create a function
CREATE OR REPLACE FUNCTION test1.process_upload(p1 text, p2 integer) RETURNS integer AS 
$$
DECLARE
	arow record;
    counter integer := 0;
BEGIN
	FOR arow IN (SELECT * FROM test1.buffer_table)
    LOOP
    	-- process this row
    	counter = counter + 1;
    END LOOP;
    
    RETURN counter;
END;
$$ LANGUAGE plpgsql;

-- test the function
select test1.process_upload('jwang', 10);


import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "buffer_table", schema = "test1")
@NamedStoredProcedureQueries({
    @NamedStoredProcedureQuery(
        name="processUpload", // query name used in your app
        procedureName = "process_upload", // name of stored proc in db
        //resultClasses = {BufferTable.class},
        parameters = {
          @StoredProcedureParameter(name="p1", type=String.class, mode=ParameterMode.IN),
          @StoredProcedureParameter(name="p2", type=Integer.class, mode=ParameterMode.IN)
        }
    )
})
public class BufferTable implements Serializable {

    @Id
    @Column(name="id")
    private Integer id;

    @Column(name="name")
    private String name;

    @Column(name="created")
    private Date created;

    @Column(name="department_name")
    private String departmentName;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    public String getDepartmentName() {
        return departmentName;
    }
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}


public int processUpload() {
    StoredProcedureQuery spQuery = em.createNamedStoredProcedureQuery("processUpload");
    // use named param - positional param can be used as well.
    spQuery.setParameter("p1", "jwang");
    spQuery.setParameter("p2", 10);

    List results = spQuery.getResultList();
    Integer result = results.size() > 0 ? (Integer) results.get(0) : null;
    log.info("Result from executing stored proc: " + result);

    return result != null ? result.intValue() : 0;
}



For more example: http://www.baeldung.com/jpa-stored-procedures
分享到:
评论

相关推荐

    hibernate-jpa-2.1-api 1.0.0.Final API

    **标题详解:** "hibernate-jpa-2.1-api 1.0.0.Final API" 这个标题指的是Hibernate对Java Persistence API (JPA) 2.1规范的实现,版本为1.0.0.Final。Hibernate是Java领域中最受欢迎的对象关系映射(ORM)框架之一...

    存储过程(Stored Procedure)

    ### 存储过程(Stored Procedure)详解 #### 一、存储过程的概念与作用 存储过程是一种预先编写并编译好的SQL语句集合,通常用于实现特定的数据库操作或逻辑处理。存储过程存储在数据库服务器中,用户可以通过指定...

    学习使用存储过程(Stored Procedure)

    ### 学习使用存储过程(Stored Procedure) 在IT领域中,存储过程(Stored Procedure)是一项重要的技术,尤其对于从事Web开发尤其是ASP编程的开发者来说,掌握如何使用存储过程至关重要。存储过程是一种预先编译并...

    Using Informatica Stored Procedure Transformation

    在数据集成领域,Informatica是一个强大的企业级ETL(提取、转换、加载)工具,而“使用Informatica存储过程转换”是它的一项重要功能。存储过程转换允许用户通过Informatica执行预先在数据库中构建的存储过程,从而...

    MySql存储过程编程.chm

    MySQL Stored Procedure Programming Advance Praise for MySQL Stored Procedure Programming Preface Objectives of This Book Structure of This Book What This Book Does Not Cover Conventions ...

    一个基于ADO.NET+COBOL+Stored Procedure的程序例子代码

    Stored Procedure是SQL Server数据库中预编译的SQL语句集合,它可以执行一系列复杂的数据库操作,如查询、插入、更新和删除数据。使用Stored Procedure有诸多优点,包括提高性能、减少网络流量、增强安全性以及提供...

    mysql存储过程编程 MySQL.Stored.Procedure.Programming

    mysql存储过程方面的圣经,以通俗的示例方法讲述mysql存储过程的深奥内容,In MySQL Stored Procedure Programming, they put that hard-won experience to good use. Packed with code examples and covering ...

    MySQL Stored Procedure Programming

    The implementation of stored procedures in MySQL 5.0 ... This book, destined to be the bible of stored procedure development, is a resource that no real MySQL programmer can afford to do without.

    一个基于C#实现的后台运行 any stored procedure 的类库源码程序

    标题中的“一个基于C#实现的后台运行 any stored procedure 的类库源码程序”表明这是一个C#编程项目,它的核心功能是能够在后台执行SQL Server的存储过程(stored procedure)。存储过程是预编译的SQL语句集合,...

    sybase stored procedure

    在 Sybase 中,存储过程分为两种类型:用户定义的存储过程(User-Defined Stored Procedures)和系统存储过程(System Stored Procedures)。用户定义的存储过程是由开发者创建的,可以根据业务需求进行定制。系统...

    mysql stored procedure programming PDF

    涉及的关键字包括CREATE PROCEDURE、CALL、ALTER PROCEDURE、DROP PROCEDURE等。 3. 参数和变量:学习存储过程中的输入参数、输出参数以及局部变量的使用和声明。参数允许存储过程接收外部的输入值,而局部变量则...

    DB2 Stored Procedure 存储过程教程

    DB2存储过程是数据库管理中的一个重要概念,它是一组为了完成特定功能的SQL语句集,可以在数据库中预先编译并存储。这个教程是专为初学者设计的,旨在帮助快速掌握DB2存储过程的创建、调用以及相关概念。...

    解决OracleRAC集群下创建SDE时报Stored procedures错误问题.docx

    在Oracle RAC(Real Application Clusters)环境下,安装和配置ArcGIS Desktop的SDE(Spatial Database Extensions)数据库连接时,可能会遇到"Stored procedures"错误。这个问题通常与Oracle RAC的特性有关,即其...

    A tool to generate class files to implement stored procedure

    标题中的"A tool to generate class files to implement stored procedure"指的是一个软件工具,它的主要功能是自动生成Java类文件,这些类文件被设计用来实现数据库中的存储过程。存储过程是在数据库中预编译的SQL...

    A class to call stored procedures that do not return records

    2. **准备SQL命令**:对于不返回记录的存储过程,SQL命令通常是`CALL procedure_name()`。类中可能包含一个方法用于设置这个命令,可能接受存储过程名称作为参数。 3. **执行存储过程**:使用数据库连接对象执行SQL...

    存储过程讲解存储过程(Stored Procedure), 是一组为了完成特定功能的SQL 语句,集经编译后

    存储过程(Stored Procedure), 是一组为了完成特定功能的SQL 语句,集经编译后 存储在数据库中,用户通过指定存储过程的名字并给出参数,如果该存储过程带有参数来执行它, 在SQL Server 的系列版本中,存储过程...

    Oracle Database 11g Administration Workshop II (数据库管理-课堂练习II 学生指南第3册)

    ### Oracle Database 11g Administration Workshop II:关键知识点解析 #### 标题解析与扩展知识点 **Oracle Database 11g Administration Workshop II**这一标题直接指向了Oracle Database 11g版本下的高级数据库...

    UNIDAC 6.4.16 XE10~10.1

    Bug with stored procedure parameters is fixed Bug with setting query parameters as a string is fixed PostgreSQL data provider Bug with stored procedure parameters is fixed SQLite data provider Bug ...

Global site tag (gtag.js) - Google Analytics