【转】hibernate调用mysql5.0存储过程

【转】

准备工作:
1.hibernate3
到这下载hibernate3:http://sourceforge.net/project/showfiles.phpgroup_id=40712&package_id=127784&release_id=403223
2.mysql (注意一定要用mysql5.0和最新驱动)
 mysql官方网站http://www.mysql.com/

None.gif
None.gif
1 .建张表
None.gif
CREATE   TABLE  `proctest` (
None.gif  `id` 
int ( 11 NOT   NULL  auto_increment,
None.gif  `Name` 
varchar ( 20 default   '''''' ,
None.gif  `age` 
int ( 11 default   NULL ,
None.gif  `address` 
varchar ( 50 default   '' ,
None.gif  
PRIMARY   KEY   (`id`)
None.gif) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;
None.gif插入几条记录
None.gif
INSERT   INTO  `proctest`  VALUES  ( 1 , ' tom ' , 22 , 'http://www.blogjava.net ' );
None.gif
INSERT   INTO  `proctest`  VALUES  ( 2 , ' wujun ' , 25 , 'http://www.blogjava.net/wujun ' );
None.gif
INSERT   INTO  `proctest`  VALUES  ( 3 , ' jerry ' , 30 , ' 深圳 ' );
None.gif
INSERT   INTO  `proctest`  VALUES  ( 4 , ' wujun ' , 40 , ' 南昌 ' );
None.gif创建存储过程
None.gif
-- 这只是一个例子,就来个简单存储过程
None.gif
create   PROCEDURE  testProc()
None.gif
begin
None.gif   
select   *   from  proctest;
None.gif
end ;



打开eclipce新建个java工程,记的把hiberbate3类库也一起加进去..
看下结构图:
r_hibernate.bmp

1.新建UserVO.java文件

None.gif package  net.wj.proc.vo;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  UserVO  dot.gif {
InBlock.gif    
private   int  id;
InBlock.gif    
private  String name;
InBlock.gif    
private   int  age;
InBlock.gif    
private  String address;
InBlock.gif    
public  UserVO()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{}

顺便把它相对应的配置文件也写上。。
UserVO.hbm.xml 


None.gif
xml version="1.0" encoding='UTF-8' ?>
None.gif
DOCTYPE hibernate-mapping PUBLIC
None.gif                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
None.gif                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" 
>
None.gif
None.gif
<!-- DO NOT EDIT: This is a generated file that is synchronized --&gt
None.gif
<!-- by MyEclipse Hibernate tool integration.                   --&gt
None.gif
<!-- Created Sat Apr 22 17:08:56 CST 2006                         --&gt
None.gif
<hibernate-mapping>
None.gif
None.gif    
<class name="net.wj.proc.vo.UserVO" table="proctest">
None.gif        
<id name="id" column="id">
None.gif            
<generator class="native"/>
None.gif        
id>
None.gif        
None.gif        
<property name="name" column="name" type="string" />
None.gif        
<property name="age" column="age" type="integer" />
None.gif        
<property name="address" column="address" type="string" />
None.gif
None.gif    
class>
None.gif
None.gif    
<!--sql查询--&gt
None.gif     
<sql-query name="select">
None.gif     
select {usr.*} from proctest usr ]]>
None.gif     
<return alias="usr" class="net.wj.proc.vo.UserVO" />
None.gif     
sql-query>
None.gif
None.gif     
<!--调用存储过程就在这里配了--&gt
None.gif    
<sql-query name="getUser" callable="true">
None.gif     
<return alias="user" class="net.wj.proc.vo.UserVO">
None.gif     
None.gif     
<return-property name="id" column="id" />
None.gif      
<return-property name="name" column="name" />
None.gif       
<return-property name="age" column="age" />
None.gif        
<return-property name="address" column="address" />
None.gif     
return>
None.gif     
<!--这里就是我们刚刚创建的存储过程名--&gt
None.gif     {call testProc()}
None.gif     
sql-query>
None.gif
hibernate-mapping>
None.gif


测试代码

None.gif package  net.wj.proc.test;
None.gif
None.gif
None.gif
import  java.util.List;
None.gif
None.gif
import  org.hibernate. * ;
None.gif
import  org.hibernate.cfg. * ;
None.gif
import  net.wj.proc.vo. * ;
None.gif
import  org.apache.log4j. * ;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  ProcTest  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

InBlock.gif    Logger log
=Logger.getLogger(this.getClass());
InBlock.gif    
public ProcTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{}
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        System.out.print(
"start.............................");
InBlock.gif        ProcTest tt
=new ProcTest();
InBlock.gif       
// tt.LoadAll();
InBlock.gif       
// tt.ExampleSelect();
InBlock.gif
       tt.ExampleProc();
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
//得到Session,
InBlock.gif
    public Session  getSession()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Configuration cfg 
= new Configuration().configure();
InBlock.gif            SessionFactory sf
=cfg.buildSessionFactory();
InBlock.gif            Session ss
= sf.openSession();
InBlock.gif            
return ss;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(Exception ee)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.out.print(
"失败"+ee.getMessage());
InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif      
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//这里调我们在UserVO.hbm.xml
InBlock.gif    
//sql-query 写上的name属性getUser
InBlock.gif
    public void ExampleProc()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Session ss
=this.getSession();
InBlock.gif        List li
=ss.getNamedQuery("getUser").list();
InBlock.gif        
for(int i=0;i<li.size();i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UserVO vo
=(UserVO)li.get(i);
InBlock.gif            log.info(
"name:"+vo.getName());
InBlock.gif            log.info(
"age"+vo.getAge());
InBlock.gif            log.info(
"address"+vo.getAddress());
ExpandedSubBlockEnd.gif        }

InBlock.gif        ss.close();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//配置文件的sql查询
InBlock.gif
    public void ExampleSelect()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif           Session ss
=this.getSession();
InBlock.gif           List li
= ss.getNamedQuery("select").list();
InBlock.gif        
InBlock.gif           
for(int i=0;i<li.size();i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif            UserVO vo
=(UserVO)li.get(i);
InBlock.gif            log.info(
"name:"+vo.getName());
InBlock.gif            log.info(
"age"+vo.getAge());
InBlock.gif            log.info(
"address"+vo.getAddress());
ExpandedSubBlockEnd.gif           }

InBlock.gif           ss.close();  
ExpandedSubBlockEnd.gif    }
}



记的用最新的驱动:
要不然可能会报这个错

None.gif Exception in thread  " main "  org.hibernate.exception.GenericJDBCException: could not execute query
None.gif    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:
91 )
None.gif    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:
79 )
None.gif    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:
43 )
None.gif    at org.hibernate.loader.Loader.doList(Loader.java:
2148 )
None.gif    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:
2029 )
None.gif    at org.hibernate.loader.Loader.list(Loader.java:
2024 )
None.gif    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:
111 )
None.gif    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:
1674 )
None.gif    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:
147 )
None.gif    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:
164 )
None.gif    at net.wj.proc.test.ProcTest.ExampleProc(ProcTest.java:
45 )
None.gif    at net.wj.proc.test.ProcTest.main(ProcTest.java:
22 )
None.gifCaused by: java.sql.SQLException: Callable statments not supported.
None.gif    at com.mysql.jdbc.Connection.prepareCall(Connection.java:
1278 )
None.gif    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:
439 )
None.gif    at org.hibernate.jdbc.AbstractBatcher.prepareCallableQueryStatement(AbstractBatcher.java:
115 )
None.gif    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:
1557 )
None.gif    at org.hibernate.loader.Loader.doQuery(Loader.java:
661 )
None.gif    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
224 )
None.gif    at org.hibernate.loader.Loader.doList(Loader.java:
2145 )
None.gif    dot.gif 
8  more
None.gif
09 : 38 : 18 , 837   INFO SessionFactoryImpl: 153   -  building session factory
None.gif
09 : 38 : 18 , 917   WARN Configurator: 126   -  No configuration found. Configuring ehcache from ehcache - failsafe.xml found in the classpath: jar:file: / E: / lib / hibernate3 / ehcache - 1.1 .jar !/ ehcache - failsafe.xml
None.gif
09 : 38 : 21 , 951   INFO SessionFactoryObjectFactory: 82   -  Not binding factory to JNDI, no JNDI name configured
ExpandedBlockStart.gif

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-159267/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-159267/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值