您的位置:首页 > 其它

hibernate 3.1 调用存储过程

2009-02-09 16:31 323 查看
今天写了一下hibernate 3.1 调用存储过程的例子.遇到了两个问题.

程序中使用sql2000数据库.数据库脚本如下:

--创建库
create database hibernateproc
go
use hibernateproc;
go
--创建表
create table tuser(
uid int identity,
name varchar(50) not null,
pass varchar(50) not NULL,
primary KEY(uid)
)
go
--插入测试数据
insert into tuser values('chenyiwan','bajyie');
insert into tuser VALUES('abcde','bbbbb');
insert into tuser values('ccdef','abcdee');
insert into tuser values('abcdefff','cccc');
go

--查询数据

select * from tuser

go
--创建存储过程
create proc getUserList
as
begin
select * from tuser;
end

go

--执行存储过程如下

exec getUserList



核心代码如下

Tuser.hbm.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.coffee.hibernate.pojo.Tuser" table="tuser"
schema="dbo" catalog="hibernateproc">
<id name="uid" type="java.lang.Integer">
<column name="uid" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<property name="pass" type="java.lang.String">
<column name="pass" length="50" not-null="true" />
</property>
</class>
<sql-query name="getUserList" callable="true">

<return alias="Tuser" class="org.coffee.hibernate.pojo.Tuser">

<return-property name="uid" column="uid" />

<return-property name="name" column="name" />

<return-property name="pass" column="pass" />

</return>
{call getUserList()}
</sql-query>

</hibernate-mapping>





hibernate.cfg.xml如下

<?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">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433;databasename=hibernateproc
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">SQL2000</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="show_sql">true</property>
<mapping resource="org/coffee/hibernate/pojo/Tuser.hbm.xml" />

</session-factory>

</hibernate-configuration>



测试代码如下:

package org.coffee.hibernate.test;

import java.util.List;

import org.coffee.hibernate.pojo.Tuser;
import org.coffee.hibernate.pojo.TuserDAO;

public class TestHibernateProc {
public static void main(String args[]) throws Exception{
TuserDAO tudao=new TuserDAO();

System.out.print(tudao);
List list=tudao.getSession().getNamedQuery("getUserList").list();
Tuser t=null;
System.out.println("执行存储过程结果如下:");
for(int i=0;i<list.size();i++){
t=(Tuser)list.get(i);
System.out.print("姓名:"+t.getName()+" 密码:"+t.getPass()+"/n");
}
}
}

执行结果如下:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.coffee.hibernate.pojo.TuserDAO@80f4cbHibernate: {call getUserList()}
执行存储过程结果如下:
姓名:chenyiwan 密码:bajyie
姓名:abcde 密码:bbbbb
姓名:ccdef 密码:abcdee
姓名:abcdefff 密码:cccc



源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: