Skip to content

Instantly share code, notes, and snippets.

@hugithordarson
Created August 11, 2015 22:00
Show Gist options
  • Save hugithordarson/d2cdaf60a6ee12aa0d17 to your computer and use it in GitHub Desktop.
Save hugithordarson/d2cdaf60a6ee12aa0d17 to your computer and use it in GitHub Desktop.
/**
* @return The number of rows matching the given expression.
*/
public static long count( ObjectContext oc, Class<? extends DataObject> entityClass, Expression expression ) {
StringBuilder b = new StringBuilder();
b.append( "SELECT count(a) " );
b.append( " FROM " );
b.append( oc.getEntityResolver().getObjEntity( entityClass ).getName() );
b.append( " a" );
if( expression != null ) {
b.append( " WHERE " );
b.append( expression.toEJBQL( "a" ) );
}
String queryString = b.toString();
EJBQLQuery query = new EJBQLQuery( queryString );
List result = oc.performQuery( query );
for( Object object : result ) {
return (Long)object;
}
throw new IllegalStateException( "Execution should never reach here (count is returned in the above loop)" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment