javax.persistence.StoredProcedureQuery Java Examples

The following examples show how to use javax.persistence.StoredProcedureQuery. 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: SQLServerStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 7 votes vote down vote up
@Test
public void testStoredProcedureRefCursor() {
    try {
        doInJPA(entityManager -> {
            StoredProcedureQuery query = entityManager
                .createStoredProcedureQuery("post_comments")
            .registerStoredProcedureParameter(1, Long.class, ParameterMode.IN)
            .registerStoredProcedureParameter(2, Class.class, ParameterMode.REF_CURSOR)
            .setParameter(1, 1L);

            query.execute();
            List<Object[]> postComments = query.getResultList();
            assertNotNull(postComments);
        });
    } catch (Exception e) {
        assertTrue(Pattern.compile("Dialect .*? not known to support REF_CURSOR parameters").matcher(e.getCause().getMessage()).matches());
    }
}
 
Example #2
Source File: PostgreSQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 7 votes vote down vote up
@Test
public void testStoredProcedureOutParameterCloseStatement() {
    doInJPA(entityManager -> {
        try {
            StoredProcedureQuery query = entityManager
            .createStoredProcedureQuery("count_comments")
            .registerStoredProcedureParameter("postId", Long.class, ParameterMode.IN)
            .registerStoredProcedureParameter("commentCount", Long.class, ParameterMode.OUT)
            .setParameter("postId", 1L);

            try {
                query.execute();
                Long commentCount = (Long) query.getOutputParameterValue("commentCount");

                assertEquals(Long.valueOf(2), commentCount);
            } finally {
                query.unwrap(ProcedureOutputs.class).release();
            }

            CallableStatement callableStatement = ReflectionUtils.getFieldValue(query.unwrap(ProcedureOutputs.class), "callableStatement");
            assertTrue(callableStatement.isClosed());
        } catch (SQLException e) {
            fail(e.getMessage());
        }
    });
}
 
Example #3
Source File: TestNamedStoredProcedureQuery.java    From HibernateTips with MIT License 7 votes vote down vote up
@Test
public void calculate() {

	EntityManager em = emf.createEntityManager();
       em.getTransaction().begin();
       
	StoredProcedureQuery query = em.createNamedStoredProcedureQuery("calculate");
	query.setParameter("x", 1.23d);
	query.setParameter("y", 4d);
	query.execute();
	Double sum = (Double) query.getOutputParameterValue("sum");
	log.info("Calculation result: 1.23 + 4 = " + sum);

       em.getTransaction().commit();
       em.close();
}
 
Example #4
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 #5
Source File: RemoteProcedureRegister.java    From hibernate-demos with Apache License 2.0 6 votes vote down vote up
public void registerMarshaller() {
	if ( registered ) {
		return;
	}

	StoredProcedureQuery storedProcedureQuery = em.createStoredProcedureQuery( "RegisterMarshallersTask" );
	storedProcedureQuery.execute();
	registered = true;
}
 
Example #6
Source File: MySQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testStoredProcedureRefCursor() {
    try {
        doInJPA(entityManager -> {
            StoredProcedureQuery query = entityManager.createStoredProcedureQuery("post_comments");
            query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
            query.registerStoredProcedureParameter(2, Class.class, ParameterMode.REF_CURSOR);
            query.setParameter(1, 1L);

            query.execute();
            List<Object[]> postComments = query.getResultList();
            assertNotNull(postComments);
        });
    } catch (Exception e) {
        assertTrue(Pattern.compile("Dialect .*? not known to support REF_CURSOR parameters").matcher(e.getCause().getMessage()).matches());
    }
}
 
Example #7
Source File: MySQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunction() {
    try {
        doInJPA(entityManager -> {
            StoredProcedureQuery query = entityManager.createStoredProcedureQuery("fn_count_comments");
            query.registerStoredProcedureParameter("postId", Long.class, ParameterMode.IN);

            query.setParameter("postId", 1L);

            Long commentCount = (Long) query.getSingleResult();
            assertEquals(Long.valueOf(2), commentCount);
        });
    } catch (Exception e) {
        assertTrue(Pattern.compile("PROCEDURE high_performance_java_persistence.fn_count_comments does not exist").matcher(e.getCause().getCause().getMessage()).matches());
    }
}
 
Example #8
Source File: SQLServerStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testStoredProcedureOutParameter() {
    doInJPA(entityManager -> {
        StoredProcedureQuery query = entityManager
            .createStoredProcedureQuery("count_comments")
            .registerStoredProcedureParameter(
                "postId", Long.class, ParameterMode.IN)
            .registerStoredProcedureParameter(
                "commentCount", Long.class, ParameterMode.OUT)
            .setParameter("postId", 1L);

        query.execute();

        Long commentCount = (Long) query.getOutputParameterValue("commentCount");
        assertEquals(Long.valueOf(2), commentCount);
    });
}
 
Example #9
Source File: MySQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcedureCallMultipleOutParameter() {
    doInJPA(entityManager -> {
        StoredProcedureQuery query = entityManager
            .createStoredProcedureQuery("getStatistics")
            .registerStoredProcedureParameter(
                    "A", Long.class, ParameterMode.OUT)
            .registerStoredProcedureParameter(
                    "B", Long.class, ParameterMode.OUT)
            .registerStoredProcedureParameter(
                    "C", Long.class, ParameterMode.OUT);

        query.execute();

        Long a = (Long) query
                .getOutputParameterValue("A");
        Long b = (Long) query
                .getOutputParameterValue("B");
        Long c = (Long) query
                .getOutputParameterValue("C");
    });
}
 
Example #10
Source File: PostgreSQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 6 votes vote down vote up
@Test
public void testStoredProcedureOutParameter() {
    doInJPA(entityManager -> {
        try {
            StoredProcedureQuery query = entityManager
                .createStoredProcedureQuery("count_comments")
                .registerStoredProcedureParameter("postId", Long.class, ParameterMode.IN)
                .registerStoredProcedureParameter("commentCount", Long.class, ParameterMode.OUT)
                .setParameter("postId", 1L);
            query.execute();
            Long commentCount = (Long) query.getOutputParameterValue("commentCount");
            assertEquals(Long.valueOf(2), commentCount);

            ProcedureOutputs procedureOutputs = query.unwrap(ProcedureOutputs.class);
            CallableStatement callableStatement = ReflectionUtils.getFieldValue(procedureOutputs, "callableStatement");
            assertFalse(callableStatement.isClosed());

            procedureOutputs.release();

            assertTrue(callableStatement.isClosed());
        } catch (SQLException e) {
            fail(e.getMessage());
        }
    });
}
 
Example #11
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 #12
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 #13
Source File: TransactionScopedEntityManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) {
    checkBlocking();
    try (EntityManagerResult emr = getEntityManager()) {
        return emr.em.createStoredProcedureQuery(procedureName, resultSetMappings);
    }
}
 
Example #14
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) {
	checkOpen();
	try {
		try {
			return createStoredProcedureCall( procedureName, resultSetMappings );
		}
		catch (UnknownSqlResultSetMappingException unknownResultSetMapping) {
			throw new IllegalArgumentException( unknownResultSetMapping.getMessage(), unknownResultSetMapping );
		}
	}
	catch ( RuntimeException e ) {
		throw exceptionConverter.convert( e );
	}
}
 
Example #15
Source File: TransactionScopedEntityManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses) {
    checkBlocking();
    try (EntityManagerResult emr = getEntityManager()) {
        return emr.em.createStoredProcedureQuery(procedureName, resultClasses);
    }
}
 
Example #16
Source File: TransactionScopedEntityManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createNamedStoredProcedureQuery(String name) {
    checkBlocking();
    try (EntityManagerResult emr = getEntityManager()) {
        return emr.em.createNamedStoredProcedureQuery(name);
    }
}
 
Example #17
Source File: TransactionScopedEntityManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createStoredProcedureQuery(String procedureName) {
    checkBlocking();
    try (EntityManagerResult emr = getEntityManager()) {
        return emr.em.createStoredProcedureQuery(procedureName);
    }
}
 
Example #18
Source File: StoredProcedureJpaRepositoryImpl.java    From ueboot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/***
 * 设置参数
 * @param storedProcedureQuery 存储
 * @param namedParams 对象
 */
private void setParameter(StoredProcedureQuery storedProcedureQuery, NamedParams namedParams){
    Map<String,Object> params=namedParams.getParameters ();
    if(CollectionUtils.isEmpty (params)){
        return;
    }
    for(Map.Entry<String,Object> entry: params.entrySet ()){
        storedProcedureQuery.setParameter (entry.getKey (),entry.getValue ());
    }
}
 
Example #19
Source File: MySQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Test
public void testStoredProcedureOutParameter() {
    doInJPA(entityManager -> {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("count_comments");
        query.registerStoredProcedureParameter("postId", Long.class, ParameterMode.IN);
        query.registerStoredProcedureParameter("commentCount", Long.class, ParameterMode.OUT);

        query.setParameter("postId", 1L);

        query.execute();
        Long commentCount = (Long) query.getOutputParameterValue("commentCount");
        assertEquals(Long.valueOf(2), commentCount);
    });
}
 
Example #20
Source File: MySQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Test
public void testStoredProcedureReturnValue() {
    doInJPA(entityManager -> {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("post_comments");
        query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);

        query.setParameter(1, 1L);

        List<Object[]> postComments = query.getResultList();
        assertEquals(2, postComments.size());
    });
}
 
Example #21
Source File: PostgreSQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Test
public void testStoredProcedureOutParameterDefaultClose() {
    doInJPA(entityManager -> {
        StoredProcedureQuery query = entityManager
        .createStoredProcedureQuery("count_comments")
        .registerStoredProcedureParameter("postId", Long.class, ParameterMode.IN)
        .registerStoredProcedureParameter("commentCount", Long.class, ParameterMode.OUT)
        .setParameter("postId", 1L);
        query.execute();
        Long commentCount = (Long) query.getOutputParameterValue("commentCount");

        assertEquals(Long.valueOf(2), commentCount);
    });
}
 
Example #22
Source File: PostgreSQLStoredProcedureTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Test
public void testStoredProcedureRefCursor() {
    doInJPA(entityManager -> {
        StoredProcedureQuery query = entityManager
            .createStoredProcedureQuery("post_comments")
            .registerStoredProcedureParameter(1, void.class, ParameterMode.REF_CURSOR)
            .registerStoredProcedureParameter(2, Long.class, ParameterMode.IN)
            .setParameter(2, 1L);

        List<Object[]> postComments = query.getResultList();
        assertEquals(2, postComments.size());
    });
}
 
Example #23
Source File: UserRepositoryIntegrationTests.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void plainJpa21() {

	StoredProcedureQuery proc = em.createStoredProcedureQuery("plus1inout");
	proc.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
	proc.registerStoredProcedureParameter(2, Integer.class, ParameterMode.OUT);

	proc.setParameter(1, 1);
	proc.execute();

	assertThat(proc.getOutputParameterValue(2), is((Object) 2));
}
 
Example #24
Source File: StoredProcedureService.java    From hibernate-demos with Apache License 2.0 5 votes vote down vote up
public Integer updateBoardWithMessage(Long messageId) {
	register.registerMarshaller();

	StoredProcedureQuery storedProcedureQuery = em.createStoredProcedureQuery( "UpdateUserBoardTask" );
	storedProcedureQuery.registerStoredProcedureParameter( "messageId", Long.class, ParameterMode.IN );
	storedProcedureQuery.setParameter( "messageId", messageId );

	return (Integer) storedProcedureQuery.getSingleResult();
}
 
Example #25
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createNamedStoredProcedureQuery(final String name) {
    final Timer timer = Op.createNamedStoredProcedureQuery.start(this.timer, this);
    try {
        return getEntityManager().createNamedStoredProcedureQuery(name);
    } finally {
        timer.stop();
    }
}
 
Example #26
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createStoredProcedureQuery(final String procedureName) {
    final Timer timer = Op.createNamedStoredProcedureQuery.start(this.timer, this);
    try {
        return getEntityManager().createStoredProcedureQuery(procedureName);
    } finally {
        timer.stop();
    }
}
 
Example #27
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createStoredProcedureQuery(final String procedureName, final Class... resultClasses) {
    final Timer timer = Op.createStoredProcedureQuery.start(this.timer, this);
    try {
        return getEntityManager().createStoredProcedureQuery(procedureName, resultClasses);
    } finally {
        timer.stop();
    }
}
 
Example #28
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public StoredProcedureQuery createStoredProcedureQuery(final String procedureName, final String... resultSetMappings) {
    final Timer timer = Op.createStoredProcedureQuery.start(this.timer, this);
    try {
        return getEntityManager().createStoredProcedureQuery(procedureName, resultSetMappings);
    } finally {
        timer.stop();
    }
}
 
Example #29
Source File: StoredProcedureLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void findCarsByYearNamedProcedure() {
    final StoredProcedureQuery findByYearProcedure = entityManager.createNamedStoredProcedureQuery("findByYearProcedure");
    final StoredProcedureQuery storedProcedure = findByYearProcedure.setParameter("p_year", 2015);
    storedProcedure.getResultList()
        .forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
}
 
Example #30
Source File: StoredProcedureLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void findCarsByYearNoNamed() {
    final StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class)
        .registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN)
        .setParameter(1, 2015);
    storedProcedure.getResultList()
        .forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
}