Java Hibernate calls Oracle stored procedures

Source: Internet
Author: User

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
    1. Session session =hibernatesessionfactory.getsession ();
    2. SQLQuery query = Session.createsqlquery ("{ call Tj (?)}");//The stored procedure is called here
    3. Query.setstring (1, "ddd");
    4. List List =query.list ();
    5. Session.close ();

Java Hibernate calls Oracle stored procedures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.