blogjava's web log

blogjava's web log
...

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/


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



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


1.新建UserVO.java文件

package  net.wj.proc.vo;

public   class  UserVO  {
    
private   int  id;
    
private  String name;
    
private   int  age;
    
private  String address;
    
public  UserVO()
    
{}
顺便把它相对应的配置文件也写上。。
UserVO.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" 
>

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->
<!-- Created Sat Apr 22 17:08:56 CST 2006                         -->
<hibernate-mapping>

    
<class name="net.wj.proc.vo.UserVO" table="proctest">
        
<id name="id" column="id">
            
<generator class="native"/>
        
</id>
        
        
<property name="name" column="name" type="string" />
        
<property name="age" column="age" type="integer" />
        
<property name="address" column="address" type="string" />

    
</class>

    
<!--sql查询-->
     
<sql-query name="select">
     
<![CDATA[select {usr.*} from proctest usr ]]>
     
<return alias="usr" class="net.wj.proc.vo.UserVO" />
     
</sql-query>

     
<!--调用存储过程就在这里配了-->
    
<sql-query name="getUser" callable="true">
     
<return alias="user" class="net.wj.proc.vo.UserVO">
     
     
<return-property name="id" column="id" />
      
<return-property name="name" column="name" />
       
<return-property name="age" column="age" />
        
<return-property name="address" column="address" />
     
</return>
     
<!--这里就是我们刚刚创建的存储过程名-->
     {call testProc()}
     
</sql-query>
</hibernate-mapping>

测试代码
package net.wj.proc.test;


import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.*;
import net.wj.proc.vo.*;
import org.apache.log4j.*;


public class ProcTest {

    
/**
     * 
@param args
     
*/

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

    
    
//得到Session,
    public Session  getSession()
    
{
        
try
        
{
            Configuration cfg 
= new Configuration().configure();
            SessionFactory sf
=cfg.buildSessionFactory();
            Session ss
= sf.openSession();
            
return ss;

        }

        
catch(Exception ee)
        
{
            System.out.print(
"失败"+ee.getMessage());
            
return null;
        }

      
    }

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

        ss.close();
    }

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

           ss.close();  
    }
}


记的用最新的驱动:
要不然可能会报这个错
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:
91)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:
79)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:
43)
    at org.hibernate.loader.Loader.doList(Loader.java:
2148)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:
2029)
    at org.hibernate.loader.Loader.list(Loader.java:
2024)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:
111)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:
1674)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:
147)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:
164)
    at net.wj.proc.test.ProcTest.ExampleProc(ProcTest.java:
45)
    at net.wj.proc.test.ProcTest.main(ProcTest.java:
22)
Caused by: java.sql.SQLException: Callable statments not supported.
    at com.mysql.jdbc.Connection.prepareCall(Connection.java:
1278)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:
439)
    at org.hibernate.jdbc.AbstractBatcher.prepareCallableQueryStatement(AbstractBatcher.java:
115)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:
1557)
    at org.hibernate.loader.Loader.doQuery(Loader.java:
661)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
224)
    at org.hibernate.loader.Loader.doList(Loader.java:
2145)
     
8 more
09:38:18,837  INFO SessionFactoryImpl:153 - building session factory
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
09:38:21,951  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: 
{call testProc()}
09:38:22,482  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: S1C00
09:38:22,482 ERROR JDBCExceptionReporter:72 - Callable statments not supported.

源代码http://www.blogjava.net/Files/wujun/Proc.rar

是不是挺简单的.

posted on 2006-05-20 11:21 record java and net 阅读(6189) 评论(14)  编辑  收藏 所属分类: java

评论

# re: hibernate调用mysql5.0存储过程小记 2006-05-24 10:36 Tin

哈哈,好文,谢谢。不过是否用过没有返回值的存储过程?我发现在Hibernate里面用没有返回值的存储过程总会出错,不知是否解决?  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-05-24 11:58 吴某人-不断地学习

看老兄的博客。。是牛人啊。。

还是用session.connection得到jdbc实例进行调用吧。

好象那样不行。。我试了也报错。。

  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-05-30 11:14 老鼠

呵呵,受教:)

我把您这篇文章转走了,您愿意么?

如果有疑问,请在此留言或给我邮件:cammy.sun@hotmail.com.


谢谢您:)  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-05-30 11:24 吴某人-不断地学习

可以啊

把转载在那里告诉我一下。。

呵呵  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-05-30 12:13 老鼠

:P

呵呵,我把它转tech.it168.com去了:p

您同意么:P

好象您也上ITPUB的哟:),我也是那里的常客:p

:shake:  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-05-30 17:22 吴某人-不断地学习

呵呵。。

  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-06-07 10:19 sunqiqi

不错。赞一个。  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-07-20 22:39 何为

报Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
这个错。
我的驱动版本是3.1.13的,还是报这个错啊

请问你用的是什么版本呢  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-07-21 10:00 吴某人-不断地学习

用最新的驱动。。
3.1.13 的可能不行。

下最新 5。0的驱动就可以了
http://dev.mysql.com/downloads/  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2006-10-23 14:04 tt

不过创建存储过程
-- 这只是一个例子,就来个简单存储过程
create PROCEDURE testProc()
begin
select * from proctest;
end ;
像这样子,只有一个语句的,好像可以,要是多个语句的在MYSQL管理里面没有办法编辑,老出问题,
  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2007-07-10 16:12 超超

此代码对了吗?为什么我在其它地方看到的代码,他们都说要调用session.connection()去调用存储过程呢?  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2007-11-02 10:35 joyou

朋友 你的测试 是否正确 否则看了之后 也不清楚 谢谢 :)  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记 2008-12-12 14:36 stringtz

请问数据库的驱动放什么地方
我照你的做为什么报错是这样的
start.............................Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method getName() is undefined for the type UserVO
The method getAge() is undefined for the type UserVO
The method getAddress() is undefined for the type UserVO

at net.wj.proc.test.ProcTest.ExampleProc(ProcTest.java:54)
at net.wj.proc.test.ProcTest.main(ProcTest.java:23)  回复  更多评论   

# re: hibernate调用mysql5.0存储过程小记[未登录] 2010-01-14 12:40 AAA

能有点追求吗》Hibernate如何调用out类型的参数写了吗,  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论