Java Code Examples for javax.persistence.StoredProcedureQuery#setParameter()

The following examples show how to use javax.persistence.StoredProcedureQuery#setParameter() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: UserRepositoryIntegrationTests.java    From spring-data-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void plainJpa21_entityAnnotatedCustomNamedProcedurePlus1IO() {

	StoredProcedureQuery proc = em.createNamedStoredProcedureQuery("User.plus1");

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

	assertThat(proc.getOutputParameterValue("res"), is((Object) 2));
}