I am trying to paginate search results without loading all matching Objects into memory. The search can match over 10,000 Objects, but a page should only display from 10 to 50 Objects at a time. There are several ways I could do this, but none of them work when the Object.class is specified in the where clause. Has anyone overcome this problem?
I have tried the following:
Code:
session.createQuery("from Resource as resource where resource.class = Resource AND <user specified conditions...>").iterate();
---- or ----
Query q1 = session.createQuery("from Resource as resource where resource.class = Resource AND <user specified conditions...>");
q1.setFirstResult(10);
q1.setMaxResults(20);
int total = (Integer)session.createQuery("select count(*) from Resource as resource where resource.class = Resource AND <user specified conditions...>").uniqueResult();
Hibernate generates invalid SQL in all of these cases.
I really appreciate your help with this issue.
I have a project at work that is in trouble if I can't figure this one out.
Thanks!
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelpHibernate version: 3.1.2
Mapping documents:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.nkics.socialworkprn.swan.domain.Resource" table="RESOURCE">
<cache usage="read-write"/>
<id name="id" type="int">
<column name="RESOURCE_ID" />
<generator class="native" />
</id>
<property name="firstName" type="string">
<column name="RESOURCE_FIRST_NAME" length="40" not-null="true" />
</property>
<property name="middleName" type="string">
<column name="RESOURCE_MIDDLE_NAME" length="40" />
</property>
<property name="lastName" type="string">
<column name="RESOURCE_LAST_NAME" length="40" not-null="true" />
</property>
<joined-subclass name="com.nkics.socialworkprn.swan.domain.Staff" table="STAFF">
<key column="RESOURCE_ID" />
<property name="left" type="timestamp">
<column name="STAFF_LEFT" length="23" />
</property>
<joined-subclass name="com.nkics.socialworkprn.swan.domain.Coordinator" table="COORDINATOR">
<key column="RESOURCE_ID" />
<property name="coordinatorCreated" type="timestamp">
<column name="COORDINATOR_CREATED" length="23" />
</property>
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public class GeneralTests extends AbstractSpringTest
{
public void testCountQuery()
{
Session session = getSessionFactory().openSession();
Query query = session.createQuery("select count(*) from Resource as resource where resource.class = Resource");
Object result = query.uniqueResult();
logger.info("Result is of type: "+result.getClass().getName());
logger.info("Result value: "+result.toString());
}
}
Full stack trace of any exception that occurs:Code:
2006-11-14 14:58:14,873 DEBUG [com.mchange.v2.sql.SqlUtils] - Attempted to convert SQLException to SQLException. Leaving it alone. [SQLState: S0002; errorCode: 107]
com.microsoft.sqlserver.jdbc.SQLServerException: The column prefix 'resource0_2_' does not match with a table name or alias name used in the query.
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.doExecuteQuery(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(Unknown Source)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
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)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1129)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:749)
at com.nkics.socialworkprn.swan.test.persistence.GeneralTests.testCountQuery(GeneralTests.java:34)
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 junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using: Microsoft SQL Server Desktop Edition
The generated SQL (show_sql=true):Code:
Hibernate: select count(*) as col_0_0_ from RESOURCE resource0_ where case when resource0_2_.RESOURCE_ID is not null then 2 when resource0_1_.RESOURCE_ID is not null then 1 when resource0_.RESOURCE_ID is not null then 0 end=0