JAVA-9610 Verify Stored Procedures with Hibernate

This commit is contained in:
anuragkumawat 2022-04-19 23:08:18 +05:30
parent 66f829cd8d
commit 3800a6a858
2 changed files with 51 additions and 3 deletions

View File

@ -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)

View File

@ -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<Foo> allFoos3 = spQuery.getResultList();
for (Foo foo : allFoos3) {
LOGGER.info("getAllFoos() StoredProcedureQuery result : {}", foo.getName());
}
assertEquals(allFoos3.size(), fooService.findAll().size());
}
@Test
@ -117,5 +139,14 @@ public class FooStoredProceduresLiveTest {
LOGGER.info("getFoosByName() using Native Query : found => {}", foo.toString());
}
StoredProcedureQuery spQuery = entityManager.
createNamedStoredProcedureQuery("GetFoosByName")
.setParameter("fooName", "NewFooName");
List<Foo> allFoosByName3 = spQuery.getResultList();
assertEquals(1, allFoosByName3.size());
for (Foo foo : allFoosByName3) {
LOGGER.info("getFoosByName() using StoredProcedureQuery : found => {}", foo.toString());
assertEquals("NewFooName", foo.getName());
}
}
}