创建存储SqlServer2005存储过程的语句
create procedure pro_GetAllStudent
as
 select * from student
goStudent.hbm.xml里的配置
 <sql-query name="pro_GetAllStudent" callable="true">
     <return alias="student" class="com.Student">
     <return-property name="stuid" column="stuid"/>
     <return-property name="stuname" column="stuname"/>
     <return-property name="stuaddress" column="stuaddress"/>
     </return>
     {call pro_GetAllStudent()}
    </sql-query>
Test.java里的main方法调用
SessionFactory sf=HibernateSessionFactory.getSessionFactory();
Session session=sf.openSession();
//System.out.println(session==null);
 List list = session.getNamedQuery("pro_GetAllStudent").list();   
  
        for (int i = 0; i < list.size(); i++) {   
  
            Student user = (Student) list.get(i);       
  
            System.out.print("stuid: " + user.getStuid());   
  
            System.out.print("stuname: " + user.getStuname());   
  
            System.out.println("stuaddress: " + user.getStuaddress());   
  
        }   
以下是异常信息:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at com.Test.main(Test.java:20)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 找不到存储过程 'pro_GetAllStudent'。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.sendExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(Unknown Source)
at org.hibernate.dialect.SybaseDialect.getResultSet(SybaseDialect.java:186)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:193)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1784)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)请高手帮忙解决,感激不尽!

解决方案 »

  1.   

    存储过程  创建到SqlServer2005里面的啊
      

  2.   

    会不会是驱动不行啊?印象中这些驱动都是在sql server2000上用的,不知道到2005上会不会有问题。
    你试着直接调用存储过程看看,类似这样的:
    tx = session.beginTransaction();
    Connection con=session.connection();
    String procedure = "{call pro_GetAllStudent()}";
    CallableStatement cstmt = con.prepareCall(procedure);
    cstmt.executeUpdate();
    tx.commit();