论坛首页 Java企业应用论坛

为何在SQLserver中hibernate查不到结果集时,会抛错?

浏览 7535 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-10-18  
情况描述:
代码在Oracle环境是没有问题的,转到SQLServer后,当用下面代码查某个表的第一页数据时,如果表中有记录是没有问题的,没有记录就会抛下面的错误。
代码:
public PageResult list(final DetachedCriteria detachedCriteria, Page page)
		throws HibernateException {

	List list = null;
	log.debug("条件分页查询.");
	try {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		Criteria criteria = detachedCriteria.getExecutableCriteria(session);			
		// 得到总记录数
		Integer totalCount = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();			
		page.setRecordTotal(totalCount.intValue());
		criteria.setProjection(null);
		// 得到指定页的记录集
		criteria.setFirstResult(page.getBeginRecordNo()).setMaxResults(page.getPageSize());
		list = criteria.list();
	} catch (HibernateException he) {
		log.error("条件分页查询出错.");
		he.printStackTrace();
	}
	PageResult pr = new PageResult(page, list);
	return pr;

}



表中没数据是出错信息:
Hibernate: /* criteria query */ select count(*) as y0_ from javauser.dbo.TD_EXAMDATA this_
Hibernate: /* criteria query */ select top 0 this_.EXAMID as EXAMID1_0_, this_.EXAMNO as EXAMNO1_0_, this_.CITYCODE as CITYCODE1_0_, this_.CITYNAME as CITYNAME1_0_, this_.ZONECODE as ZONECODE1_0_, this_.ZONENAME as ZONENAME1_0_, this_.STREETCODE as STREETCODE1_0_, this_.STREETNAME as STREETNAME1_0_, this_.BLOCKNAME as BLOCKNAME1_0_, this_.ACCESSINGDATE as ACCESSI10_1_0_, this_.STARTTIME as STARTTIME1_0_, this_.ENDTTIME as ENDTTIME1_0_, this_.INQUIRER as INQUIRER1_0_, this_.CHECKER as CHECKER1_0_, this_.FILLINER as FILLINER1_0_, this_.FILLINDATE as FILLINDATE1_0_, this_.STATUS as STATUS1_0_, this_.ORGNAME as ORGNAME1_0_ from javauser.dbo.TD_EXAMDATA this_
10:51:47,218 ERROR JDBCExceptionReporter:72 - ResultSet may only be accessed in a forward direction.
10:51:47,218 ERROR BaseDAO:466 - 条件分页查询出错.
org.hibernate.exception.GenericJDBCException: [color=red]could not execute query[/color]
	at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
	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.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
	at $Proxy0.list(Unknown Source)
	at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
	at com.susun.common.hibernate.base.BaseDAO.list(BaseDAO.java:464)
	at com.susun.smoke.struts.FillinAction.list(FillinAction.java:81)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
	at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
	at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at com.susun.common.hibernate.HibernateFilter.doFilter(HibernateFilter.java:71)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:831)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1203)
	at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: [color=red]ResultSet may only be accessed in a forward direction.[/color]	at net.sourceforge.jtds.jdbc.JtdsResultSet.checkScrollable(JtdsResultSet.java:316)
	at net.sourceforge.jtds.jdbc.JtdsResultSet.absolute(JtdsResultSet.java:713)
	at org.hibernate.loader.Loader.advance(Loader.java:1470)
	at org.hibernate.loader.Loader.getResultSet(Loader.java:1674)
	at org.hibernate.loader.Loader.doQuery(Loader.java:662)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
	at org.hibernate.loader.Loader.doList(Loader.java:2145)
	... 40 more

环境:
SqlServer2K-sp4 + jtds1.2 + Hibernate3.1.3


配置:
Hiberntae.cfg.xml:
<session-factory>
	<!-- 设定SQL方言 -->
	<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

	<!-- 数据库连接参数-->
	<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
	<property name="connection.url">jdbc:jtds:sqlserver://192.168.1.168:1433/javauser</property>
	<property name="connection.username">sa</property>
	<property name="connection.password">javauser</property>
	<!-- hibernate自带连接池参数 -->
	<property name="connection.pool_size">2</property>
	<!-- 设定Hibernate自动管理session上下文-->
	<property name="current_session_context_class">thread</property>
	<!-- 禁用二级缓存 -->
	<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

	<!-- 在控制台显示执行的SQL -->
	<property name="show_sql">true</property>
	<property name="hibernate.use_sql_comments">true</property>

	<mapping resource=smoke/hibernate/domain/TdExamdata.hbm.xml" />



</session-factory>


持久类映射:
TdExamdata.hbm.xml
<hibernate-mapping>
       <class name="com.susun.smoke.hibernate.domain.TdExamdata" table="TD_EXAMDATA" schema="dbo" catalog="javauser">
        <id name="examid" type="string">
            <column name="EXAMID" length="32" />
            <generator class="assigned" />
        </id>
        <property name="examno" type="string">
            <column name="EXAMNO" length="20" />
        </property>
        <property name="citycode" type="string">
            <column name="CITYCODE" length="32" />
        </property>
        <property name="cityname" type="string">
            <column name="CITYNAME" length="100" />
        </property>
        <property name="zonecode" type="string">
            <column name="ZONECODE" length="32" />
        </property>
        <property name="zonename" type="string">
            <column name="ZONENAME" length="100" />
        </property>
        <property name="streetcode" type="string">
            <column name="STREETCODE" length="32" />
        </property>
        <property name="streetname" type="string">
            <column name="STREETNAME" length="100" />
        </property>
        <property name="blockname" type="string">
            <column name="BLOCKNAME" length="200" />
        </property>
        <property name="accessingdate" type="java.util.Date">
            <column name="ACCESSINGDATE"/>
        </property>
        <property name="starttime" type="string">
            <column name="STARTTIME" length="20" />
        </property>
        <property name="endttime" type="string">
            <column name="ENDTTIME" length="20" />
        </property>
        <property name="inquirer" type="string">
            <column name="INQUIRER" length="40" />
        </property>
        <property name="checker" type="string">
            <column name="CHECKER" length="20" />
        </property>
        <property name="filliner" type="string">
            <column name="FILLINER" length="20" />
        </property>
        <property name="fillindate" type="java.util.Date">
            <column name="FILLINDATE"/>
        </property>
        <property name="status" type="string">
            <column name="STATUS" length="1" />
        </property>
        <property name="orgname" type="string">
            <column name="ORGNAME" length="100" />
        </property>
    </class>
</hibernate-mapping>



Google过,没有获得有帮助的信息,但从网上的资看来似乎是jTDS的问题。
   发表时间:2006-10-18  
criteria.setFirstResult(x)
在sqlserver中,这个x不能为负数。
但是在oracle中,x为负数则没关系。它会当作从0开始的。
你可以debug一下,看看x是否为负数了。

贴的代码太乱了一些。
0 请登录后投票
   发表时间:2006-10-18  
together 写道
criteria.setFirstResult(x)
在sqlserver中,这个x不能为负数。
但是在oracle中,x为负数则没关系。它会当作从0开始的。
你可以debug一下,看看x是否为负数了。

贴的代码太乱了一些。


晕。。。就是这个问题!!!感谢!
由于一直用Oracle开发,没有发现Page类的这个BUG。

另:报的错也令人摸不着边。
0 请登录后投票
   发表时间:2006-10-27  
mark
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics