Java Code Examples for javax.persistence.entitymanager#createStoredProcedureQuery()

The following examples show how to use javax.persistence.entitymanager#createStoredProcedureQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestStoredProcedureQuery.java    From HibernateTips with MIT License 7 votes vote down vote up
@Test
public void calculate() {
	log.info("... calculate ...");
	EntityManager em = emf.createEntityManager();
       em.getTransaction().begin();
       
	// define the stored procedure
	StoredProcedureQuery query = em.createStoredProcedureQuery("calculate");
	query.registerStoredProcedureParameter("x", Double.class, ParameterMode.IN);
	query.registerStoredProcedureParameter("y", Double.class, ParameterMode.IN);
	query.registerStoredProcedureParameter("sum", Double.class, ParameterMode.OUT);
	
	// set input parameter
	query.setParameter("x", 1.23d);
	query.setParameter("y", 4d);
	
	// call the stored procedure and get the result
	query.execute();
	Double sum = (Double) query.getOutputParameterValue("sum");
	log.info("Calculation result: 1.23 + 4 = " + sum);

       em.getTransaction().commit();
       em.close();
}
 
Example 2
Source File: SharedEntityManagerCreatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void deferredStoredProcedureQueryWithIndexedParameters() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager targetEm = mock(EntityManager.class);
	StoredProcedureQuery query = mock(StoredProcedureQuery.class);
	given(emf.createEntityManager()).willReturn(targetEm);
	given(targetEm.createStoredProcedureQuery("x")).willReturn(query);
	willReturn("y").given(query).getOutputParameterValue(0);
	willReturn("z").given(query).getOutputParameterValue(2);
	given(targetEm.isOpen()).willReturn(true);

	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	StoredProcedureQuery spq = em.createStoredProcedureQuery("x");
	spq.registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
	spq.registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
	spq.registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
	spq.execute();
	assertEquals("y", spq.getOutputParameterValue(0));
	try {
		spq.getOutputParameterValue(1);
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
	assertEquals("z", spq.getOutputParameterValue(2));

	verify(query).registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
	verify(query).registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
	verify(query).registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
	verify(query).execute();
	verify(targetEm).close();
	verifyNoMoreInteractions(query);
	verifyNoMoreInteractions(targetEm);
}
 
Example 3
Source File: SharedEntityManagerCreatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void deferredStoredProcedureQueryWithNamedParameters() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager targetEm = mock(EntityManager.class);
	StoredProcedureQuery query = mock(StoredProcedureQuery.class);
	given(emf.createEntityManager()).willReturn(targetEm);
	given(targetEm.createStoredProcedureQuery("x")).willReturn(query);
	willReturn("y").given(query).getOutputParameterValue("a");
	willReturn("z").given(query).getOutputParameterValue("c");
	given(targetEm.isOpen()).willReturn(true);

	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	StoredProcedureQuery spq = em.createStoredProcedureQuery("x");
	spq.registerStoredProcedureParameter("a", String.class, ParameterMode.OUT);
	spq.registerStoredProcedureParameter("b", Number.class, ParameterMode.IN);
	spq.registerStoredProcedureParameter("c", Object.class, ParameterMode.INOUT);
	spq.execute();
	assertEquals("y", spq.getOutputParameterValue("a"));
	try {
		spq.getOutputParameterValue("b");
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
	assertEquals("z", spq.getOutputParameterValue("c"));

	verify(query).registerStoredProcedureParameter("a", String.class, ParameterMode.OUT);
	verify(query).registerStoredProcedureParameter("b", Number.class, ParameterMode.IN);
	verify(query).registerStoredProcedureParameter("c", Object.class, ParameterMode.INOUT);
	verify(query).execute();
	verify(targetEm).close();
	verifyNoMoreInteractions(query);
	verifyNoMoreInteractions(targetEm);
}
 
Example 4
Source File: SharedEntityManagerCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void deferredStoredProcedureQueryWithNamedParameters() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager targetEm = mock(EntityManager.class);
	StoredProcedureQuery query = mock(StoredProcedureQuery.class);
	given(emf.createEntityManager()).willReturn(targetEm);
	given(targetEm.createStoredProcedureQuery("x")).willReturn(query);
	willReturn("y").given(query).getOutputParameterValue("a");
	willReturn("z").given(query).getOutputParameterValue("c");
	given(targetEm.isOpen()).willReturn(true);

	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	StoredProcedureQuery spq = em.createStoredProcedureQuery("x");
	spq.registerStoredProcedureParameter("a", String.class, ParameterMode.OUT);
	spq.registerStoredProcedureParameter("b", Number.class, ParameterMode.IN);
	spq.registerStoredProcedureParameter("c", Object.class, ParameterMode.INOUT);
	spq.execute();
	assertEquals("y", spq.getOutputParameterValue("a"));
	try {
		spq.getOutputParameterValue("b");
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
	assertEquals("z", spq.getOutputParameterValue("c"));

	verify(query).registerStoredProcedureParameter("a", String.class, ParameterMode.OUT);
	verify(query).registerStoredProcedureParameter("b", Number.class, ParameterMode.IN);
	verify(query).registerStoredProcedureParameter("c", Object.class, ParameterMode.INOUT);
	verify(query).execute();
	verify(targetEm).close();
	verifyNoMoreInteractions(query);
	verifyNoMoreInteractions(targetEm);
}
 
Example 5
Source File: SharedEntityManagerCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void deferredStoredProcedureQueryWithIndexedParameters() {
	EntityManagerFactory emf = mock(EntityManagerFactory.class);
	EntityManager targetEm = mock(EntityManager.class);
	StoredProcedureQuery query = mock(StoredProcedureQuery.class);
	given(emf.createEntityManager()).willReturn(targetEm);
	given(targetEm.createStoredProcedureQuery("x")).willReturn(query);
	willReturn("y").given(query).getOutputParameterValue(0);
	willReturn("z").given(query).getOutputParameterValue(2);
	given(targetEm.isOpen()).willReturn(true);

	EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
	StoredProcedureQuery spq = em.createStoredProcedureQuery("x");
	spq.registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
	spq.registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
	spq.registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
	spq.execute();
	assertEquals("y", spq.getOutputParameterValue(0));
	try {
		spq.getOutputParameterValue(1);
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
	assertEquals("z", spq.getOutputParameterValue(2));

	verify(query).registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
	verify(query).registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
	verify(query).registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
	verify(query).execute();
	verify(targetEm).close();
	verifyNoMoreInteractions(query);
	verifyNoMoreInteractions(targetEm);
}