Hibernate calls the Stored Procedure

Source: Internet
Author: User

 

Use hibernate to call a stored procedure.

 

Public class StuInfo {

Private int id;

Private String stuName;

Private String stuNo;

Private int stuAge;

Private String stuId;

Private String stuSeat;

Private String stuAddress; setters (); getters ();}

 

 

Corresponding database table:

 

If exists (select * from sysobjects where name = 'stuinfo ')

Drop table stuInfo

Create table stuInfo/* create student information table **/

(

StuName varchar (20) not null, -- name, not empty

StuNo char (6) not null, -- Student ID, not empty

StuAge int not null, -- year long, int Is 4 by default Length

StuId numeric (18, 0 ),

StuSeat smallint, -- seat car, auto-incrementing

StuAddress text -- address can be blank

)

-- Add a column to stuInfo

Alter table stuInfo add id int identity (1, 1) primary key;

Create a stored procedure:

 

-- Stored Procedure

If exists (select name from sysobjects where name = 'proc _ stuinfo' and type = 'P ')

Drop proc proc_stuInfo

Go

Create proc proc_stuInfo

As

Select * from stuInfo

Go

-- Call a stored procedure

Exec proc_stuInfo;

 

Several Methods for calling stored procedures in hibernate.

 

First: name query

 

<SQL-query name = "getStuInfo" callable = "true">

<Return alias = "stuInfo" class = "com. hkrt. domain. StuInfo">

<Return-property name = "id" column = "id"/>

<Return-property name = "stuName" column = "stuName"/>

<Return-property name = "stuAge" column = "stuAge"/>

<Return-property name = "stuNo" column = "stuNo"/>

<Return-property name = "stuSeat" column = "stuSeat"/>

<Return-property name = "stuAddress" column = "stuAddress"/>

<Return-property name = "stuId" column = "stuId"/>

</Return>

{Call proc_stuInfo ()}

</SQL-query>

List li = session. getNamedQuery ("getStuInfo"). list ();

System. out. println (li. get (0); Type 2: jdbc

 

System. out. println ("jdbc call -------------");

Connection conn = session. connection ();

ResultSet rs = null;

CallableStatement call;

Try {

Call = conn. prepareCall ("{Call proc_stuInfo ()}");

Rs = call.exe cuteQuery ();

While (rs. next ()){

System. out. println (rs. getString (1 ));

System. out. println (rs. getString (2 ));

System. out. println (rs. getString (3 ));

System. out. println (rs. getString (4 ));

System. out. println (rs. getString (5 ));

System. out. println (rs. getString (6 ));

System. out. println (rs. getString (7 ));

System. out. println ("------------------");

}

} Catch (SQLException e ){

E. printStackTrace ();

}

 

Third: the simplest one

 

SQLQuery query = session. createSQLQuery ("{call proc_stuInfo ()}"). addEntity (StuInfo. class );

List list = query. list ();

System. out. println (list. get (0 ));

Note: In the third call, you must add addEntity (); otherwise, no data is returned.

 

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.