[jboss-cvs] jboss-seam/src/main/org/jboss/seam/framework ...
Gavin King
gavin.king at jboss.com
Fri May 18 21:36:37 EDT 2007
User: gavin
Date: 07/05/18 21:36:37
Modified: src/main/org/jboss/seam/framework EntityQuery.java
HibernateEntityQuery.java Query.java
Log:
efficient way to tell if there are more results
Revision Changes Path
1.15 +30 -6 jboss-seam/src/main/org/jboss/seam/framework/EntityQuery.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: EntityQuery.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/framework/EntityQuery.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- EntityQuery.java 23 Apr 2007 13:57:57 -0000 1.14
+++ EntityQuery.java 19 May 2007 01:36:37 -0000 1.15
@@ -32,20 +32,34 @@
}
}
+ @Override
+ @Transactional
+ public boolean isNextExists()
+ {
+ return resultList!=null &&
+ resultList.size() > getMaxResults();
+ }
+
+
@Transactional
@Override
public List getResultList()
{
- if (isAnyParameterDirty())
+ if ( isAnyParameterDirty() )
{
refresh();
}
- if ( resultList==null )
+ initResultList();
+ return truncResultList(resultList);
+ }
+
+ private void initResultList()
+ {
+ if (resultList==null)
{
javax.persistence.Query query = createQuery();
resultList = query==null ? null : query.getResultList();
}
- return resultList;
}
@Transactional
@@ -56,13 +70,18 @@
{
refresh();
}
+ initSingleResult();
+ return singleResult;
+ }
+
+ private void initSingleResult()
+ {
if ( singleResult==null)
{
javax.persistence.Query query = createQuery();
singleResult = query==null ?
null : query.getSingleResult();
}
- return singleResult;
}
@Transactional
@@ -73,13 +92,18 @@
{
refresh();
}
+ initResultCount();
+ return resultCount;
+ }
+
+ private void initResultCount()
+ {
if ( resultCount==null )
{
javax.persistence.Query query = createCountQuery();
resultCount = query==null ?
null : (Long) query.getSingleResult();
}
- return resultCount;
}
@Override
@@ -118,7 +142,7 @@
setParameters( query, getQueryParameterValues(), 0 );
setParameters( query, getRestrictionParameterValues(), getQueryParameterValues().size() );
if ( getFirstResult()!=null) query.setFirstResult( getFirstResult() );
- if ( getMaxResults()!=null) query.setMaxResults( getMaxResults() );
+ if ( getMaxResults()!=null) query.setMaxResults( getMaxResults()+1 ); //add one, so we can tell if there is another page
if ( getHints()!=null )
{
for ( Map.Entry<String, String> me: getHints().entrySet() )
1.12 +42 -7 jboss-seam/src/main/org/jboss/seam/framework/HibernateEntityQuery.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: HibernateEntityQuery.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/framework/HibernateEntityQuery.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- HibernateEntityQuery.java 26 Feb 2007 19:24:50 -0000 1.11
+++ HibernateEntityQuery.java 19 May 2007 01:36:37 -0000 1.12
@@ -37,38 +37,73 @@
@Override
public List getResultList()
{
- if (resultList==null || isAnyParameterDirty())
+ if ( isAnyParameterDirty() )
+ {
+ refresh();
+ }
+ initResultList();
+ return truncResultList(resultList);
+ }
+
+ private void initResultList()
+ {
+ if (resultList==null)
{
org.hibernate.Query query = createQuery();
resultList = query==null ? null : query.list();
}
- return resultList;
+ }
+
+ @Override
+ @Transactional
+ public boolean isNextExists()
+ {
+ return resultList!=null &&
+ resultList.size() > getMaxResults();
}
@Transactional
@Override
public Object getSingleResult()
{
- if (singleResult==null || isAnyParameterDirty())
+ if (isAnyParameterDirty())
+ {
+ refresh();
+ }
+ initSingleResult();
+ return singleResult;
+ }
+
+ private void initSingleResult()
+ {
+ if (singleResult==null)
{
org.hibernate.Query query = createQuery();
singleResult = query==null ?
null : query.uniqueResult();
}
- return singleResult;
}
@Transactional
@Override
public Long getResultCount()
{
- if ( resultCount==null || isAnyParameterDirty() )
+ if (isAnyParameterDirty())
+ {
+ refresh();
+ }
+ initResultCount();
+ return resultCount;
+ }
+
+ private void initResultCount()
+ {
+ if (resultCount==null)
{
org.hibernate.Query query = createCountQuery();
resultCount = query==null ?
null : (Long) query.uniqueResult();
}
- return resultCount;
}
@Override
@@ -106,7 +141,7 @@
setParameters( query, getQueryParameterValues(), 0 );
setParameters( query, getRestrictionParameterValues(), getQueryParameterValues().size() );
if ( getFirstResult()!=null) query.setFirstResult( getFirstResult() );
- if ( getMaxResults()!=null) query.setMaxResults( getMaxResults() );
+ if ( getMaxResults()!=null) query.setMaxResults( getMaxResults()+1 ); //add one, so we can tell if there is another page
if ( getCacheable()!=null ) query.setCacheable( getCacheable() );
if ( getCacheRegion()!=null ) query.setCacheRegion( getCacheRegion() );
if ( getFetchSize()!=null ) query.setFetchSize( getFetchSize() );
1.27 +13 -5 jboss-seam/src/main/org/jboss/seam/framework/Query.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Query.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/framework/Query.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -b -r1.26 -r1.27
--- Query.java 1 May 2007 16:43:51 -0000 1.26
+++ Query.java 19 May 2007 01:36:37 -0000 1.27
@@ -219,11 +219,7 @@
return getFirstResult()!=null && getFirstResult()!=0;
}
- public boolean isNextExists()
- {
- return getResultList()!=null &&
- getResultList().size() == getMaxResults();
- }
+ public abstract boolean isNextExists();
public void setFirstResult(Integer firstResult)
{
@@ -328,5 +324,17 @@
{
this.restrictionParameterValues = restrictionParameterValues;
}
+ protected List truncResultList(List results)
+ {
+ Integer mr = getMaxResults();
+ if ( mr!=null && results.size() > mr )
+ {
+ return results.subList(0, mr-1);
+ }
+ else
+ {
+ return results;
+ }
+ }
}
More information about the jboss-cvs-commits
mailing list