Skip to content

Commit

Permalink
add query which returns limited number of results
Browse files Browse the repository at this point in the history
  • Loading branch information
chris grzegorczyk committed Nov 30, 2011
1 parent 7ac5c86 commit 0b27b39
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -492,7 +492,7 @@ public static Function<ServiceStatusType, CheckException> transformToExceptions(
public static Collection<CheckException> lookup( final ServiceConfiguration config ) {
final EntityTransaction db = Entities.get( CheckException.class );
try {
final List<CheckException> res = Entities.query( new CheckException( config.getName( ) ) );
final List<CheckException> res = Entities.query( new CheckException( config.getName( ) ), true, 1 );
db.commit( );
return res;
} catch ( final Exception ex ) {
Expand Down
Expand Up @@ -233,6 +233,21 @@ public static <T> List<T> query( final T example, final boolean readOnly ) {
return Lists.newArrayList( Sets.newHashSet( resultList ) );
}

@SuppressWarnings( { "unchecked", "cast" } )
public static <T> List<T> query( final T example, final boolean readOnly, final int maxResults ) {
final Example qbe = Example.create( example ).enableLike( MatchMode.EXACT );
final List<T> resultList = ( List<T> ) getTransaction( example ).getTxState( ).getSession( )
.createCriteria( example.getClass( ) )
.setReadOnly( readOnly )
.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY )
.setCacheable( true )
.add( qbe )
.setMaxResults( maxResults )
.setFetchSize( maxResults )
.list( );
return Lists.newArrayList( Sets.newHashSet( resultList ) );
}

public static <T> T uniqueResult( final T example ) throws TransactionException, NoSuchElementException {
try {
final Object pk = resolvePrimaryKey( example );
Expand Down

0 comments on commit 0b27b39

Please sign in to comment.