Oracle Stored Procedure:
CREATE OR REPLACE PROCEDURE AddNumbers(
num1 IN NUMBER,
num2 IN NUMBER,
result OUT NUMBER
) AS
BEGIN
result := num1 + num2;
END;
/
Entity Class
import javax.persistence.Entity;
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureParameter;
@Entity
@NamedStoredProcedureQuery(
name = "AddNumbers",
procedureName = "AddNumbers",
parameters = {
@StoredProcedureParameter(name = "num1", mode = ParameterMode.IN, type = Integer.class),
@StoredProcedureParameter(name = "num2", mode = ParameterMode.IN, type = Integer.class),
@StoredProcedureParameter(name = "result", mode = ParameterMode.OUT, type = Integer.class)
}
)
public class Calculation {
@Id
private Long id;
public Calculation() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Calling the StoredProcedure using Hibernate
package org.code2care.examples;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.procedure.ProcedureCall;
public class HibernateStoredProcedureExample {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
try {
ProcedureCall procedureCall = session.createStoredProcedureCall("AddNumbers");
procedureCall.registerParameter("num1", Integer.class, ParameterMode.IN).bindValue(2);
procedureCall.registerParameter("num2", Integer.class, ParameterMode.IN).bindValue(3);
procedureCall.registerParameter("result", Integer.class, ParameterMode.OUT);
procedureCall.execute();
Integer result = (Integer) procedureCall.getOutputParameterValue("result");
System.out.println("Result: " + result);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
}
}
Output:
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- [Fix] Microsoft Teams Error code - 2:-1012 - Teams
- Android is starting optimizing... app 1 of 1 - Android
- Test internet speed using macOS Terminal command - MacOS
- How to Open TextEdit on Mac - MacOS
- How to generate client id and secret to register SharePoint App with OAuth - SharePoint
- [fix] Loading class com.mysql.jdbc.Driver is deprecated - MySQL
- cURL DELETE Request with Examples - cURL
- Fix: 403 Forbidden/Access Denied Error during Windows Software Updates - Windows