1.hibernate calling stored procedures various methods
Http://www.cnblogs.com/jerryxing/archive/2012/04/28/2475762.html
If the underlying database (such as Oracle) supports stored procedures, you can also perform bulk updates through stored procedures. Stored procedures run directly in the database and are faster. A stored procedure named Batchupdatestudent () can be defined in the Oracle database with the following code:
Create or Replace procedure batchupdatestudent (p_age in number) as
Begin
Update STUDENT set age=age+1 where age>p_age;
End
The above stored procedure has a parameter p_age, which represents the age of the student, and the application can call the stored procedure as follows:
tx = Session.begintransaction ();
Connection con=session.connection ();
String procedure = "{call Batchupdatestudent (?)}";
CallableStatement cstmt = Con.preparecall (procedure);
Cstmt.setint (1,0); Set the age parameter to 0
Cstmt.executeupdate ();
Tx.commit ();
In the above code, I use Hibernate's transaction interface to declare transactions, rather than using the JDBC API to declare transactions.
3. Stored procedure with return value
Session session = This.gethibernatetemplate (). Getsessionfactory (). Getcurrentsession (); CallableStatement cs = session.connection (). Preparecall ("{Call Tj (?,?,?,?,?,?,?)}");
//The name of the stored procedure? Is the parameter passed in
//Set parameter values I'm setting up 7 parameters here.cs.setstring (1, startTime);cs.setstring (2, endTime);cs.setstring (3, "");cs.setstring (4, "5");cs.setstring (5, "Max");cs.setstring (6, "+");cs.setstring (7,orderby);
//Execute Query ResultSet rs = Cs.executequery ();While (Rs.next ()) {System.out.println (Rs.getint (1)); }The way above is to return a resultset the second method:through the powerful createsqlquery to achieve
- Session session =hibernatesessionfactory.getsession ();
- SQLQuery query = Session.createsqlquery ("{ call Tj (?)}");//The stored procedure is called here
- Query.setstring (1, "ddd");
- List List =query.list ();
- Session.close ();
Java Hibernate calls Oracle stored procedures