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!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Disable jQuery button after being click - jQuery
- SharePoint An unexpected error has occurred - Correlation ID and PowerShell Merge-SPlogfile - SharePoint
- Ubuntu Linux: Unzip a zip file using Terminal - Ubuntu
- What is ValueError: math domain error and how to fix it - Python
- Where are Notepad++ temp unsaved files stored? - NotepadPlusPlus
- Mac - How to Install VirtualBox - MacOS
- [Fix] Docker Run unknown shorthand flag: 'r' in -rm - Docker
- [fix] How to Show file extensions on all files on Mac - MacOS