diff --git a/persistence-jpa/src/main/resources/config/database/FIND_CAR_BY_YEAR_PROCEDURE.sql b/persistence-jpa/src/main/resources/config/database/FIND_CAR_BY_YEAR_PROCEDURE.sql new file mode 100644 index 0000000000..f9230aad42 --- /dev/null +++ b/persistence-jpa/src/main/resources/config/database/FIND_CAR_BY_YEAR_PROCEDURE.sql @@ -0,0 +1,7 @@ +create or replace PROCEDURE FIND_CAR_BY_YEAR ( p_year IN NUMBER, data OUT SYS_REFCURSOR ) AS + BEGIN + OPEN data FOR + SELECT ID, MODEL, YEAR + FROM CAR + WHERE YEAR = p_year; + END FIND_CAR_BY_YEAR; \ No newline at end of file diff --git a/persistence-jpa/src/main/resources/config/database/insert_cars.sql b/persistence-jpa/src/main/resources/config/database/insert_cars.sql index 5fef151c70..4879ffc79e 100644 --- a/persistence-jpa/src/main/resources/config/database/insert_cars.sql +++ b/persistence-jpa/src/main/resources/config/database/insert_cars.sql @@ -1,4 +1,5 @@ INSERT INTO CAR (ID, MODEL, YEAR) VALUES ('123456', 'Camaro', '2012'); -INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('12112', 'Fiat Panda', '2000') -INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('111000', 'Fiat Punto', '2007') -INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('3382', 'Citroen C3', '2009') +INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('12112', 'Fiat Panda', '2000'); +INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('111000', 'Fiat Punto', '2007'); +INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('3382', 'Citroen C3', '2009'); +commit; \ No newline at end of file diff --git a/persistence-jpa/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureTest.java b/persistence-jpa/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureTest.java index 61d4aca85e..e6880c4d76 100644 --- a/persistence-jpa/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureTest.java +++ b/persistence-jpa/src/test/java/com/baeldung/jpa/storedprocedure/StoredProcedureTest.java @@ -53,6 +53,16 @@ public class StoredProcedureTest { storedProcedure.getResultList().forEach(c -> Assert.assertEquals("Camaro", ((Car) c).getModel())); } + @Test + public void findCarsByYearNoNamedStored() { + StoredProcedureQuery findByYearProcedure = + entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class) + .registerStoredProcedureParameter("p_year", Integer.class, ParameterMode.IN) + .registerStoredProcedureParameter("data", Void.class, ParameterMode.REF_CURSOR).setParameter("p_year", 2015); + + findByYearProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear())); + } + @AfterClass public static void destroy() {