From 3800a6a858287219aca9c9cbaddf7a4929bd987b Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Tue, 19 Apr 2022 23:08:18 +0530 Subject: [PATCH] JAVA-9610 Verify Stored Procedures with Hibernate --- .../com/baeldung/persistence/model/Foo.java | 17 +++++++++ .../service/FooStoredProceduresLiveTest.java | 37 +++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/persistence-modules/spring-data-jpa-query-2/src/main/java/com/baeldung/persistence/model/Foo.java b/persistence-modules/spring-data-jpa-query-2/src/main/java/com/baeldung/persistence/model/Foo.java index eec15c5e56..ac79653b2b 100644 --- a/persistence-modules/spring-data-jpa-query-2/src/main/java/com/baeldung/persistence/model/Foo.java +++ b/persistence-modules/spring-data-jpa-query-2/src/main/java/com/baeldung/persistence/model/Foo.java @@ -13,6 +13,9 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.NamedNativeQueries; import javax.persistence.NamedNativeQuery; +import javax.persistence.NamedStoredProcedureQuery; +import javax.persistence.ParameterMode; +import javax.persistence.StoredProcedureParameter; import org.hibernate.envers.Audited; @@ -20,6 +23,20 @@ import org.hibernate.envers.Audited; @NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class) }) +@NamedStoredProcedureQuery( + name="GetAllFoos", + procedureName="GetAllFoos", + resultClasses = { Foo.class } + ) +@NamedStoredProcedureQuery( + name="GetFoosByName", + procedureName="GetFoosByName", + resultClasses = { Foo.class }, + parameters={ + @StoredProcedureParameter(name="fooName", type=String.class, mode=ParameterMode.IN) + } + ) + @Entity @Audited // @Proxy(lazy = false) diff --git a/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/persistence/service/FooStoredProceduresLiveTest.java b/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/persistence/service/FooStoredProceduresLiveTest.java index 8bf33c4110..f6dedfc6de 100644 --- a/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/persistence/service/FooStoredProceduresLiveTest.java +++ b/persistence-modules/spring-data-jpa-query-2/src/test/java/com/baeldung/persistence/service/FooStoredProceduresLiveTest.java @@ -1,10 +1,14 @@ package com.baeldung.persistence.service; -import java.util.List; - import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.junit.Assert.assertEquals; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.StoredProcedureQuery; + import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -17,6 +21,7 @@ import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; @@ -38,8 +43,15 @@ public class FooStoredProceduresLiveTest { private Session session; + @Autowired + @Qualifier("jpaEntityManager") + private EntityManagerFactory entityManagerFactory; + + private EntityManager entityManager; + @Before public final void before() { + entityManager = entityManagerFactory.createEntityManager(); session = sessionFactory.openSession(); Assume.assumeTrue(getAllFoosExists()); Assume.assumeTrue(getFoosByNameExists()); @@ -70,6 +82,7 @@ public class FooStoredProceduresLiveTest { @After public final void after() { session.close(); + entityManager.close(); } @Test @@ -94,6 +107,15 @@ public class FooStoredProceduresLiveTest { LOGGER.info("getAllFoos() NamedQuery result : {}", foo.getName()); } assertEquals(allFoos2.size(), fooService.findAll().size()); + + StoredProcedureQuery spQuery = + entityManager.createNamedStoredProcedureQuery("GetAllFoos"); + List allFoos3 = spQuery.getResultList(); + for (Foo foo : allFoos3) { + LOGGER.info("getAllFoos() StoredProcedureQuery result : {}", foo.getName()); + } + assertEquals(allFoos3.size(), fooService.findAll().size()); + } @Test @@ -116,6 +138,15 @@ public class FooStoredProceduresLiveTest { for (Foo foo : allFoosByName2) { LOGGER.info("getFoosByName() using Native Query : found => {}", foo.toString()); } - + + StoredProcedureQuery spQuery = entityManager. + createNamedStoredProcedureQuery("GetFoosByName") + .setParameter("fooName", "NewFooName"); + List allFoosByName3 = spQuery.getResultList(); + assertEquals(1, allFoosByName3.size()); + for (Foo foo : allFoosByName3) { + LOGGER.info("getFoosByName() using StoredProcedureQuery : found => {}", foo.toString()); + assertEquals("NewFooName", foo.getName()); + } } }