Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Spring Data JPA call a stored procedure examples     - Struts2 interceptor simulation (Programming)

- Java 8 perspective annotation types (Programming)

- Fedora 22 Server how to upgrade to Fedora 23 Beta Server (Linux)

- VMware virtual machine to install CentOS 6.2 (Linux)

- Use scripts easily install the latest Linux kernel in Ubuntu (Linux)

- Ubuntu 12.04 commonly use shortcuts finishing Share (Linux)

- Binary search is really easy as you think you do (Programming)

- KVM virtualization nested configuration (Server)

- Django Signals from practice to source code analysis (Programming)

- Linux SU command security Suggestions (Linux)

- Simple to use Linux GPG (Linux)

- Android engineers interview questions (Programming)

- MySQL Error Code Complete (Database)

- Based on Google Cloud Storage Mirroring open Maven Central Repository (Server)

- Install Visual Studio Code in Ubuntu (Linux)

- Linux crontab use (Linux)

- Create a project using Android Studio LinearLayout (Programming)

- Linux and hardware (Linux)

- Ubuntu Linux to create and increase the Swap partition tutorial (Linux)

- Grading defense against Linux server attacks (Linux)

 
         
  Spring Data JPA call a stored procedure examples
     
  Add Date : 2018-11-21      
         
         
         
  JPA connect to the database, call a stored procedure, this demand is very common. This article will address this point, how to use Spring Data JPA call a stored procedure approach.

1, stored procedures
Suppose stored procedure is as follows:

CREATE OR REPLACE PACKAGE test_pkg AS
  PROCEDURE in_only_test (inParam1 IN VARCHAR2);
  PROCEDURE in_and_out_test (inParam1 IN VARCHAR2, outParam1 OUT VARCHAR2);
END test_pkg;
/
CREATE OR REPLACE PACKAGE BODY test_pkg AS
  PROCEDURE in_only_test (inParam1 IN VARCHAR2) AS
  BEGIN
      DBMS_OUTPUT.PUT_LINE ( 'in_only_test');
  END in_only_test;
  PROCEDURE in_and_out_test (inParam1 IN VARCHAR2, outParam1 OUT VARCHAR2) AS
  BEGIN
      outParam1: = 'Woohoo Im an outparam, and this is my inparam' || inParam1;
  END in_and_out_test;
END test_pkg;
There are two stored procedures:

1) in_only_test
It requires an input parameter inParam1, but does not return a value

2) in_and_out_test
It requires an input parameter inParam1, and the return value outParam1

2, @ NamedStoredProcedureQueries
We can use @NamedStoredProcedureQueries comments to call a stored procedure.

@Entity
@Table (Name = "MYTABLE")
@NamedStoredProcedureQueries ({
        @NamedStoredProcedureQuery (Name = "in_only_test", procedureName = "test_pkg.in_only_test", parameters = {
                @StoredProcedureParameter (Mode = ParameterMode.IN, name = "inParam1", type = String.class)}),
        @NamedStoredProcedureQuery (Name = "in_and_out_test", procedureName = "test_pkg.in_and_out_test", parameters = {
                @StoredProcedureParameter (Mode = ParameterMode.IN, name = "inParam1", type = String.class),
                @StoredProcedureParameter (Mode = ParameterMode.OUT, name = "outParam1", type = String.class)})})
public class MyTable implements Serializable {
}
Key points:

Stored procedures used comments @NamedStoredProcedureQuery, and bind to a JPA table.
procedureName is the name of the stored procedure
name is the name of the stored procedure JPA
To define the stored procedure used IN / OUT parameters using annotations @StoredProcedureParameter
3. Create a database Spring Data JPA
Let's create the Spring Data JPA database:

public interface MyTableRepository extends CrudRepository {
    @Procedure (Name = "in_only_test")
    void inOnlyTest (@Param ( "inParam1") String inParam1);

    @Procedure (Name = "in_and_out_test")
    String inAndOutTest (@Param ( "inParam1") String inParam1);
}
Key points:

@Procedure The name parameter must match the name @NamedStoredProcedureQuery
@Param Must match the name parameter @StoredProcedureParameter comment
The return type must match: in_only_test stored procedure that returns a void, in_and_out_test stored procedure must return String
4, call
We can call a stored procedure:

    // Pass parameters to the stored procedure and returns a value
    String inParam = "Hi Im an inputParam";
    String outParam = myTableRepository.inAndOutTest (inParam);
    Assert.assertEquals (outParam, "Woohoo Im an outparam, and this is my inparam Hi Im an inputParam");
    // Pass parameters to the stored procedure does not return value
    myTableRepository.inOnlyTest (inParam);
5. Other tips
If the above code does not work, so to solve. Define custom Repository to call the stored procedure last night local query.

Define custom Repository:

public interface MyTableRepositoryCustom {
    void inOnlyTest (String inParam1);
}
Then we want to ensure that the primary Repository classes inherit this interface.

public interface MyTableRepository extends CrudRepository , MyTableRepositoryCustom {
}
6. Create Repository implementation class
Then create the implementation class of the Repository:

public class MyTableRepositoryImpl implements MyTableRepositoryCustom {
@PersistenceContext
private EntityManager em;

@Override
public void inOnlyTest (String inParam1) {
    this.em.createNativeQuery ( "BEGIN in_only_test (: inParam1); END;") setParameter ( "inParam1", inParam1).
            .executeUpdate ();
}
}

It can be called in a conventional manner:

@Autowired
MyTableRepository myTableRepository;
// Call a stored procedure
myTableRepository.inOnlyTest (inParam1);
It is not very simple.
     
         
         
         
  More:      
 
- GitLab remote backup of Linux Shell Scripting (Linux)
- About enhanced Linux / Unix server system security program (Linux)
- Linux more efficient than select a model epoll (Linux)
- DBCA Error: ORA-19809: limit exceeded for recovery files process (Database)
- Linux 6 use UDEV binding shared storage (Linux)
- Fedora 22 Server how to upgrade to Fedora 23 Beta Server (Linux)
- Linux System Getting Started Tutorial: permission to permanently modify the USB device in Linux (Linux)
- CentOS6 install Redis (Database)
- Struts2 form of non-use component tags (Programming)
- Linux operating system security can not be ignored (Linux)
- OpenGL Superb Learning Notes - GLSL language foundation (Programming)
- C # Future: Method Contract (Programming)
- CentOS 6.3 compile and install LNMP environment (Server)
- In addition to wget and curl, what better alternatives (Linux)
- Server Security Analysis attack on Linux (Linux)
- To install Cinnamon desktop environment on your Ubuntu 14.04 / 12.04 (Linux)
- How to install Ubuntu applications Device 7 (Linux)
- Understanding the Linux load average on other UNIX-Like systems (Linux)
- Android WebView use layman (Programming)
- After installing minimize RHEL / CentOS 7 we need to do (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.