Stored procedure with or without Hibernate ?
Hi,
I need to call many Stored Procedures in the application (mostly get data procedures). The application uses Spring, Hibernate (with annotations), JSF and Oracle. Could someone let me know the best way to access stored procedures.
1. Using Spring w/o using Hibernate (even though application has it)
2. Using Spring and Hibernate (using Hibernate to access stored procedures)
I would like to know Pros/Cons of above approach
Next,I have seen some examples using procedure access with Hibernate but most of them were simple single table single POJO example. Can someone please share example of framework for the real application scenario - I have one procedure with cursor output having 10 columns from 5 different tables + I have 10 such procedures.
Rocker
stored procedures in spring
Quote:
Originally Posted by
jakain
I had to do this a while back and just used JdbcTemplate, I was using hibernate already in the application but trying to map the stored procedure with the NamedNativeQuery annotation turned out to be a major PITA. Found using JdbcTemplate was straightforward and easier to maintain.
Example of calling a function that takes 2 NUMBER parameters and returns a NUMBER
Code:
final Object result = jdbcTemplate.execute(
"{ ? = call myPackage.someFunction(?,?)}", new CallableStatementCallback() {
public Object doInCallableStatement(final CallableStatement cs) throws SQLException {
cs.registerOutParameter (1, Types.INTEGER);
cs.setLong(2, someParam);
cs.setLong(3, anotherParam);
cs.execute();
return cs.getInt(1);
}
});
Hi,he StoredProcedure class is a superclass for object abstractions of RDBMS stored procedures. This class is abstract, and its various execute(..) methods have protected access, preventing use other than through a subclass that offers tighter typing.
The inherited sql property will be the name of the stored procedure in the RDBMS. Note that JDBC 3.0 introduces named parameters, although the other features provided by this class are still necessary in JDBC 3.0.
Here is an example of a program that calls a function, sysdate(), that comes with any Oracle database. To use the stored procedure functionality one has to create a class that extends StoredProcedure. There are no input parameters, but there is an output parameter that is declared as a date type using the class SqlOutParameter. The execute() method returns a map with an entry for each declared output parameter using the parameter name as the key.
import java.sql.Types;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.datasource.*;
import org.springframework.jdbc.object.StoredProcedure;
public class TestStoredProcedure {
public static void main(String[] args) {
TestStoredProcedure t = new TestStoredProcedure();
t.test();
System.out.println("Done!");
}
void test() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("oracle.jdbc.OracleDriver");
ds.setUrl("jdbc:oracle:thin:@localhost:1521:mydb") ;
ds.setUsername("scott");
ds.setPassword("tiger");
MyStoredProcedure sproc = new MyStoredProcedure(ds);
Map results = sproc.execute();
printMap(results);
}
private class MyStoredProcedure extends StoredProcedure {
private static final String SQL = "sysdate";
public MyStoredProcedure(DataSource ds) {
setDataSource(ds);
setFunction(true);
setSql(SQL);
declareParameter(new SqlOutParameter("date", Types.DATE));
compile();
}
public Map execute() {
// the 'sysdate' sproc has no input parameters, so an empty Map is supplied...
return execute(new HashMap());
}
}
private static void printMap(Map results) {
for (Iterator it = results.entrySet().iterator(); it.hasNext(); ) {
System.out.println(it.next());
}
}
}
Find below an example of a StoredProcedure that has two output parameters (in this case Oracle cursors).
import oracle.jdbc.driver.OracleTypes;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.object.StoredProcedure;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
public class TitlesAndGenresStoredProcedure extends StoredProcedure {
private static final String SPROC_NAME = "AllTitlesAndGenres";
public TitlesAndGenresStoredProcedure(DataSource dataSource) {
super(dataSource, SPROC_NAME);
declareParameter(new SqlOutParameter("titles", OracleTypes.CURSOR, new TitleMapper()));
declareParameter(new SqlOutParameter("genres", OracleTypes.CURSOR, new GenreMapper()));
compile();
}
public Map execute() {
// again, this sproc has no input parameters, so an empty Map is supplied...
return super.execute(new HashMap());
}
}