Merge pull request #12094 from anuragkumawat/JAVA-11474
JAVA-9610 Verify Stored Procedures with Hibernate
This commit is contained in:
commit
24389cbb20
|
@ -13,6 +13,9 @@ import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.NamedNativeQueries;
|
import javax.persistence.NamedNativeQueries;
|
||||||
import javax.persistence.NamedNativeQuery;
|
import javax.persistence.NamedNativeQuery;
|
||||||
|
import javax.persistence.NamedStoredProcedureQuery;
|
||||||
|
import javax.persistence.ParameterMode;
|
||||||
|
import javax.persistence.StoredProcedureParameter;
|
||||||
|
|
||||||
import org.hibernate.envers.Audited;
|
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 = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class),
|
||||||
@NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", 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
|
@Entity
|
||||||
@Audited
|
@Audited
|
||||||
// @Proxy(lazy = false)
|
// @Proxy(lazy = false)
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
package com.baeldung.persistence.service;
|
package com.baeldung.persistence.service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
import static org.junit.Assert.assertEquals;
|
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.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
@ -17,6 +21,7 @@ import org.junit.runner.RunWith;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
@ -38,8 +43,15 @@ public class FooStoredProceduresLiveTest {
|
||||||
|
|
||||||
private Session session;
|
private Session session;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("jpaEntityManager")
|
||||||
|
private EntityManagerFactory entityManagerFactory;
|
||||||
|
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public final void before() {
|
public final void before() {
|
||||||
|
entityManager = entityManagerFactory.createEntityManager();
|
||||||
session = sessionFactory.openSession();
|
session = sessionFactory.openSession();
|
||||||
Assume.assumeTrue(getAllFoosExists());
|
Assume.assumeTrue(getAllFoosExists());
|
||||||
Assume.assumeTrue(getFoosByNameExists());
|
Assume.assumeTrue(getFoosByNameExists());
|
||||||
|
@ -70,6 +82,7 @@ public class FooStoredProceduresLiveTest {
|
||||||
@After
|
@After
|
||||||
public final void after() {
|
public final void after() {
|
||||||
session.close();
|
session.close();
|
||||||
|
entityManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -94,6 +107,15 @@ public class FooStoredProceduresLiveTest {
|
||||||
LOGGER.info("getAllFoos() NamedQuery result : {}", foo.getName());
|
LOGGER.info("getAllFoos() NamedQuery result : {}", foo.getName());
|
||||||
}
|
}
|
||||||
assertEquals(allFoos2.size(), fooService.findAll().size());
|
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
|
@Test
|
||||||
|
@ -117,5 +139,14 @@ public class FooStoredProceduresLiveTest {
|
||||||
LOGGER.info("getFoosByName() using Native Query : found => {}", foo.toString());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue