当表为空时Hibernate sql异常
当作业表没有行时,以下代码会引发异常。
public List<Job> getAll(int currentPage, int pageSize) {
return this.sessionFactory.getCurrentSession()
.createCriteria(Job.class).addOrder(Order.asc("id"))
.setFirstResult(currentPage * pageSize).setMaxResults(pageSize)
.setFetchSize(pageSize).list();
}
我正在使用 SQL Server 和 JTDS 驱动程序。
我得到的错误是
java.sql.SQLException: ResultSet may only be accessed in a forward direction.
net.sourceforge.jtds.jdbc.JtdsResultSet.checkScrollable(JtdsResultSet.java:319)
net.sourceforge.jtds.jdbc.JtdsResultSet.absolute(JtdsResultSet.java:716)
org.apache.commons.dbcp.DelegatingResultSet.absolute(DelegatingResultSet.java:335)
org.hibernate.loader.Loader.advance(Loader.java:1469)
org.hibernate.loader.Loader.getResultSet(Loader.java:1783)
org.hibernate.loader.Loader.doQuery(Loader.java:662)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
org.hibernate.loader.Loader.doList(Loader.java:2211)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
org.hibernate.loader.Loader.list(Loader.java:2090)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
The following code causes an exception when the Job table has no rows.
public List<Job> getAll(int currentPage, int pageSize) {
return this.sessionFactory.getCurrentSession()
.createCriteria(Job.class).addOrder(Order.asc("id"))
.setFirstResult(currentPage * pageSize).setMaxResults(pageSize)
.setFetchSize(pageSize).list();
}
I am using SQL Server and the JTDS driver.
The error i get is
java.sql.SQLException: ResultSet may only be accessed in a forward direction.
net.sourceforge.jtds.jdbc.JtdsResultSet.checkScrollable(JtdsResultSet.java:319)
net.sourceforge.jtds.jdbc.JtdsResultSet.absolute(JtdsResultSet.java:716)
org.apache.commons.dbcp.DelegatingResultSet.absolute(DelegatingResultSet.java:335)
org.hibernate.loader.Loader.advance(Loader.java:1469)
org.hibernate.loader.Loader.getResultSet(Loader.java:1783)
org.hibernate.loader.Loader.doQuery(Loader.java:662)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
org.hibernate.loader.Loader.doList(Loader.java:2211)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
org.hibernate.loader.Loader.list(Loader.java:2090)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该问题与尝试对空表进行分页有关。
删除这些:
.setFirstResult(currentPage * pageSize).setMaxResults(pageSize)
.setFetchSize(页面大小)
并且您应该能够毫无问题地查询空表。
如果要对数据进行分页,请先运行常规查询,然后在知道有数据要分页后,使用查询对数据进行分页。
The issue is associated with trying to page an empty table.
drop these:
.setFirstResult(currentPage * pageSize).setMaxResults(pageSize)
.setFetchSize(pageSize)
and you should be able to query the empty table without issue.
If you want to page the data, run a regular query first, then page the data with your query after you know you have data to page.
将以下属性添加到 persistence.xml 为我解决了这个问题(jboss7,hibernate4)
更改方言的替代解决方案(我未检查) - https://forum.hibernate.org/viewtopic.php?p=2452163
Adding following property to persistence.xml solves this issue for me (jboss7, hibernate4)
Alternative solution with changed dialect (not checked by me) - https://forum.hibernate.org/viewtopic.php?p=2452163
我遇到了同样的问题,环顾了一段时间后,一位同事建议我将休眠方言从 this: 更改
为 this
,它解决了我的问题。
发帖只是为了让人们检查他们的方言。
I was having this same problem, and after looking around for a while I was adviced by a coworker to change the hibernate dialect from this:
to this
and it solved my problem.
Posting just as a note for people to check their dialect.