HIbernate calls the Oracle Stored Procedure

Source: Internet
Author: User

1. Create a stored procedure

Proc code
  1. Create or replace procedure changesalary (p_employeeid number, p_newsalary number) is
  2. Begin
  3. Update employees set salary = p_newsalary
  4. Where employee_id = p_employeeid;
  5. If SQL % notfound then
  6. Raise_application_error (-20100,'Invalidemployee id');
  7. End if;
  8. End;
  9. /
Create or replace procedure changesalary (p_employeeid number, p_newsalary number) isbegin update employees set salary = p_newsalary where employee_id = p_employeeid; if SQL % notfound then raise_application_error (-20100, 'invalidemployee id'); end if; end ;/

2. hibernate Configuration

Xml Code
  1. <?Xml Version="1.0" Encoding=UTF-8"?>
  2. <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  3. <Hibernate-configuration>
  4. <Session-factory>
  5. <Property Name="Hibernate. dialect">Org. hibernate. dialect. Oracle9Dialect</Property>
  6. <Property Name="Hibernate. connection. driver_class">Oracle. jdbc. driver. OracleDriver</Property>
  7. <Property Name="Hibernate. connection. url">Jdbc: oracle: thin: @ localhost: 1521: xe</Property>
  8. <Property Name="Hibernate. connection. username">Hr</Property>
  9. <Property Name="Hibernate. connection. password">Hr</Property>
  10. <Property Name="Hibernate. hbm2ddl. auto">Update</Property>
  11. <Property Name="Hibernate. show_ SQL">True</Property>
  12. </Session-factory>
  13. </Hibernate-configuration>
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

3. hibernate applications

(1) Connect using JDBC

Java code
  1. ImportOrg. hibernate. Query;
  2. ImportOrg. hibernate. Session;
  3. ImportOrg. hibernate. SessionFactory;
  4. ImportOrg. hibernate. cfg. Configuration;
  5. Public ClassCallSP {
  6. Public Static VoidMain (String [] args)ThrowsException {
  7. Configuration c =NewConfiguration (). configure ();
  8. SessionFactory sf = c. buildSessionFactory ();
  9. Session session = sf. openSession ();
  10. Session. beginTransaction ();
  11. Connection con = session. connection ();// Obtain JDBC connection from Session object
  12. CallableStatement cs = con. prepareCall ("{Call changesalary (?,?) }");
  13. Cs. setInt (1,100);// First parameter index start with 1
  14. Cs. setInt (2,6000);// Second parameter
  15. Cs.exe cute ();// Call stored procedure
  16. Session. getTransaction (). commit ();
  17. Session. close ();
  18. Sf. close ();
  19. }
  20. }
Import org. hibernate. query; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; public class CallSP {public static void main (String [] args) throws Exception {Configuration c = new Configuration (). configure (); SessionFactory sf = c. buildSessionFactory (); Session session = sf. openSession (); session. beginTransaction (); Connection con = session. conne Ction (); // obtain JDBC connection from Session object CallableStatement cs = con. prepareCall ("{call changesalary (?,?) } "); Cs. setInt (1,100); // first parameter index start with 1 cs. setInt (2,6000); // second parameter cs.exe cute (); // call stored procedure session. getTransaction (). commit (); session. close (); sf. close ();}}

(2) Use Native SQL

Java code
  1. ImportOrg. hibernate. Query;
  2. ImportOrg. hibernate. Session;
  3. ImportOrg. hibernate. SessionFactory;
  4. ImportOrg. hibernate. cfg. Configuration;
  5. Public ClassCallSP {
  6. Public Static VoidMain (String [] args)ThrowsException {
  7. Configuration c =NewConfiguration (). configure ();
  8. SessionFactory sf = c. buildSessionFactory ();
  9. Session session = sf. openSession ();
  10. Session. beginTransaction ();
  11. Query q = session. createSQLQuery ("{Call changesalary (?,?) }");
  12. Q. setInteger (0,100);// First parameter, index starts with 0
  13. Q. setInteger (1,4000);// Secon parameter
  14. Q.exe cuteUpdate ();
  15. Session. getTransaction (). commit ();
  16. Session. close ();
  17. Sf. close ();
  18. }
  19. }

Related Article

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.