Hibernate calls the Stored Procedure

Source: Internet
Author: User

Java code create table 'cmd' ('id' int (11) not null auto_increment, 'name' varchar (20), 'age' int (11 ), primary key ('id') create table 'prop1' ('id' int (11) not null auto_increment, 'name' varchar (20 ), 'age' int (11), primary key ('id') Simple Stored PROCEDURE Java code create PROCEDURE proc () begin select * from proalb; end; create PROCEDURE proc () begin select * from proalb; end; One method is to use hiber Nate uses the traditional xml ing method to call Java code <class name = "com. test. user "table =" proalb "> <id name =" id "column =" id "> <generator class =" native "/> </id> <property name =" name "column =" name "type =" string "/> <property name =" age "column =" age "type =" integer "/> </class> <SQL-query name = "getUser" callable = "true"> <return alias = "user" class = "com. test. user "> <return-property name =" id "column =" id "/> <return-property name =" name "Column =" name "/> <return-property name =" age "column =" age "/> </return> {call proc ()} </SQL-query> <class name = "com. test. user "table =" proalb "> <id name =" id "column =" id "> <generator class =" native "/> </id> <property name =" name "column =" name "type =" string "/> <property name =" age "column =" age "type =" integer "/> </class> <SQL-query name = "getUser" callable = "true"> <return alias = "user" class = "com. test. U Ser "> <return-property name =" id "column =" id "/> <return-property name =" name "column =" name "/> <return-property name = "age" column = "age"/> </return> {call proc ()} </SQL-query> call method Java code Session ss = HibernateSessionFactory. getSession () List li = ss. getNamedQuery ("getUser "). list (); ss. close (); Session ss = HibernateSessionFactory. getSession () List li = ss. getNamedQuery ("getUser "). list (); ss. close (); and similar Jdbc Method Java code Session session = HibernateSessionFactory. getSession (); Connection conn = session. connection (); ResultSet rs = null; CallableStatement call = conn. prepareCall ("{Call proc ()}"); rs = call.exe cuteQuery (); rs. close (); session. close (); Session session = HibernateSessionFactory. getSession (); Connection conn = session. connection (); ResultSet rs = null; CallableStatement call = conn. prepareCall ("{Call proc ()}"); rs = call.exe cuteQuery (); rs. close (); session. close (); this method is basically jdbc, but it is very simple and always feels strange ~! In addition, the powerful createSQLQuery can be used to implement o (∩) o... haha personally prefer this method Java code Session session = HibernateSessionFactory. getSession (); SQLQuery query = session. createSQLQuery ("{Call proc ()}"); List list = query. list (); session. close (); Session session = HibernateSessionFactory. getSession (); SQLQuery query = session. createSQLQuery ("{Call proc ()}"); List list = query. list (); session. close (); Use the execute method directly if there is no returned value (* ^__ ^ *) I forgot an important question: Passing parameters in the Stored Procedure Java code www.2cto. comCallableStatement call = conn. prepareCall ("{Call proc (?)} "); Call. setString (1, parameter); rs = call.exe cuteQuery (); CallableStatement call = conn. prepareCall (" {Call proc (?)} "); Call. setString (1, parameter); rs = call.exe cuteQuery (); Java code SQLQuery query = session. createSQLQuery (" {Call proc (?)} "); Query. setString (0, parameter); List list = query. list ();

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.