com.avaje.ebean
Interface SqlQuery

All Superinterfaces:
Serializable

public interface SqlQuery
extends Serializable

    

Query object for performing native SQL queries that return SqlRow's.

Firstly note that you can use your own sql queries with entity beans by using the SqlSelect annotation. This should be your first approach when wanting to use your own SQL queries.

If ORM Mapping is too tight and constraining for your problem then SqlQuery could be a good approach.

The returned SqlRow objects are similar to a LinkedHashMap with some type conversion support added.

 // its typically a good idea to use a named query 
 // and put the sql in the orm.xml instead of in your code
 
 String sql = "select id, name from customer where name like :name and status_code = :status";
 
 SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
 sqlQuery.setParameter("name", "Acme%");
 sqlQuery.setParameter("status", "ACTIVE");
 
 // execute the query returning a List of MapBean objects
 List<SqlRow> list = sqlQuery.findList();
 


Method Summary
 void cancel()
          Cancel the query if support by the underlying database and driver.
 SqlFutureList findFutureList()
          Execute find list SQL query in a background thread.
 List<SqlRow> findList()
          Execute the query returning a list.
 Map<?,SqlRow> findMap()
          Execute the query returning a map.
 Set<SqlRow> findSet()
          Execute the query returning a set.
 SqlRow findUnique()
          Execute the query returning a single row or null.
 SqlQuery setBackgroundFetchAfter(int backgroundFetchAfter)
          Set the index after which fetching continues in a background thread.
 SqlQuery setBufferFetchSizeHint(int bufferFetchSizeHint)
          A hint which for JDBC translates to the Statement.fetchSize().
 SqlQuery setFirstRow(int firstRow)
          Set the index of the first row of the results to return.
 SqlQuery setListener(SqlQueryListener queryListener)
          Set a listener to process the query on a row by row basis.
 SqlQuery setMapKey(String mapKey)
          Set the column to use to determine the keys for a Map.
 SqlQuery setMaxRows(int maxRows)
          Set the maximum number of query results to return.
 SqlQuery setParameter(int position, Object value)
          The same as bind for positioned parameters.
 SqlQuery setParameter(String name, Object value)
          The same as bind for named parameters.
 SqlQuery setTimeout(int secs)
          Set a timeout on this query.
 

Method Detail

cancel

void cancel()
Cancel the query if support by the underlying database and driver.

This must be called from a different thread to the one executing the query.


findList

List<SqlRow> findList()
Execute the query returning a list.


findSet

Set<SqlRow> findSet()
Execute the query returning a set.


findMap

Map<?,SqlRow> findMap()
Execute the query returning a map.


findUnique

SqlRow findUnique()
Execute the query returning a single row or null.

If this query finds 2 or more rows then it will throw a PersistenceException.


findFutureList

SqlFutureList findFutureList()
Execute find list SQL query in a background thread.

This returns a Future object which can be used to cancel, check the execution status (isDone etc) and get the value (with or without a timeout).

Returns:
a Future object for the list result of the query

setParameter

SqlQuery setParameter(String name,
                      Object value)
The same as bind for named parameters.


setParameter

SqlQuery setParameter(int position,
                      Object value)
The same as bind for positioned parameters.


setListener

SqlQuery setListener(SqlQueryListener queryListener)
Set a listener to process the query on a row by row basis.

It this case the rows are not loaded into the persistence context and instead can be processed by the query listener.

Use this when you want to process a large query and do not want to hold the entire query result in memory.


setFirstRow

SqlQuery setFirstRow(int firstRow)
Set the index of the first row of the results to return.


setMaxRows

SqlQuery setMaxRows(int maxRows)
Set the maximum number of query results to return.


setBackgroundFetchAfter

SqlQuery setBackgroundFetchAfter(int backgroundFetchAfter)
Set the index after which fetching continues in a background thread.


setMapKey

SqlQuery setMapKey(String mapKey)
Set the column to use to determine the keys for a Map.


setTimeout

SqlQuery setTimeout(int secs)
Set a timeout on this query.

This will typically result in a call to setQueryTimeout() on a preparedStatement. If the timeout occurs an exception will be thrown - this will be a SQLException wrapped up in a PersistenceException.

Parameters:
secs - the query timeout limit in seconds. Zero means there is no limit.

setBufferFetchSizeHint

SqlQuery setBufferFetchSizeHint(int bufferFetchSizeHint)
A hint which for JDBC translates to the Statement.fetchSize().

Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet.



Copyright © 2010. All Rights Reserved.