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,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to Change Background Color TextEdit on Mac - MacOS
- Install Eclipse IDE on M1/M2 Mac Natively - Eclipse
- How to Gzip a file directory on Mac OS X using Terminal Command - Mac-OS-X
- [Fix] Spring Tool Suite STS Code Autocomplete not working with Eclipse - Eclipse
- Mac - Open New Tab in Chrome Shortcut - Chrome
- Free Halloween Widgets with Countdown - CSS
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..] - Java
- How to print the key value pairs of a Dictionary in Python - Python