|
IBM WebSphereTM eXtreme Scale, Release 8.6 API Specification |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Query
Interface used to control entity query execution.
Use the EntityManager.createQuery(String)
method to create
a Query
. Each query instance can be used multiple times
using the EntityManager in which it was retrieved.
Each query result produces an entity, where the entity key is the row id (of type long) and the entity value contains the field results of the SELECT clause. Each query result can be included in subsequent queries.
EntityManager
Field Summary | |
---|---|
static String |
HINT_USEINDEX
Hint to the query engine to use the specified index. |
Method Summary | |
---|---|
String |
getPlan()
Return a string representation of the query plan. |
Iterator |
getResultIterator()
Execute a SELECT query and return the query results using an Iterator where each result is either an Object (for a single valued query) or Object[] (for a multiple valued query). |
Iterator |
getResultIterator(Class resultType)
Execute a SELECT query and return the query results using an entity Iterator where the entity type is determined by the resultType parameter. |
ObjectMap |
getResultMap()
Execute a SELECT query and return the query results in an ObjectMap with the results in query-specified order. |
Object |
getSingleResult()
Execute a SELECT query that returns a single result. |
Query |
setFirstResult(int startPosition)
Set the position of the first result to retrieve. |
Query |
setFlushMode(FlushModeType flushMode)
Set the flush mode type to be used when the query is executed, overriding the flush mode type set on the EntityManager. |
Query |
setForUpdate(boolean forUpdate)
Set the update intent for the query. |
Query |
setHint(String hintName,
Object value)
Set a query hint. |
Query |
setMaxResults(int maxResult)
Set the maximum number of results to retrieve. |
Query |
setParameter(int position,
Object value)
Bind an argument to a positional parameter. |
Query |
setParameter(String name,
Object value)
Bind an argument to a named parameter. |
Query |
setPartition(int partitionId)
Set the partition to route the query to. |
Query |
setResultEntityName(String entityName)
Specify the name of the query result entity. |
Field Detail |
---|
static final String HINT_USEINDEX
String Format:
<IndexHint> ::= <Entity Name> "." <AttributeName> <Entity Name> ::= ObjectGrid entity name <AttributeName> ::= An indexed attribute.Example:
Query q = em.createQuery("SELECT p FROM Person p WHERE p.name=?1 AND p.city=?2 AND p.state=?3"); q.setHint(Query.HINT_USEINDEX, "Person.city"); q.setParameter(1, "Smith"); q.setParameter(2, "Rochester"); q.setParameter(3, "MN"); Iterator it = q.getResultIterator();
setHint(String, Object)
,
Constant Field ValuesMethod Detail |
---|
ObjectMap getResultMap()
The map key is the result number (of type long) starting at 0.
The map value is of type
where each attribute and association is named based on it's ordinal
position within the query's select clause. Use the Tuple
method to retrieve the ObjectMap.getEntityMetadata()
EntityMetadata
for the Tuple object stored within
the map.
This method is the fastest method for retrieving query result data where there can be multiple
results. The name of the resulting entity can be retrieved using the
ObjectMap.getEntityMetadata()
and EntityMetadata.getName()
methods.
Example:
The following query returns 2 rows:
String ql = SELECT e.name, e.id, d from Employee e join e.dept d WHERE d.number=5 Query q = em.createQuery(ql); ObjectMap resultMap = q.getResultMap(); long rowID=0; Tuple tResult = (Tuple) resultMap.get(new Long(rowID)); while(tResult != null) { // The first attribute is name and has an attribute name of "1" // But has an ordinal position of 0. String name = (String)tResult.getAttribute(0); Integer id = (String)tResult.getAttribute(1); // Dept is an association with a name of "3", but // an ordinal position of 0 since it's the first association. // The association is always a OneToOne relationship, // so there is only one key. Tuple deptKey = tResult.getAssociation(0,0); rowID++; tResult = (Tuple) resultMap.get(new Long(rowID)); ... }
PersistenceException
- thrown if query validation fails.
This exception may be deferred until the query is run.Iterator getResultIterator()
This method is the preferred method for retrieving query results within the
EntityManager's context. The optional setResultEntityName(String)
method
can be used to name the resulting entity so that it can be used in further queries.
Example:
The following query returns 2 rows:
String ql = SELECT e.name, e.id, e.dept from Employee e WHERE e.dept.number=5 Query q = em.createQuery(ql); Iterator results = q.getResultIterator(); while(results.hasNext()) { Object[] curEmp = (Object[]) results.next(); String name = (String) curEmp[0]; Integer id = (Integer) curEmp[1]; Dept d = (Dept) curEmp[2]; ... }
Object
or Object[]
PersistenceException
- thrown if query validation fails.
This exception may be deferred until the query is run.
TransactionRequiredException
- if invoked without an active transaction.Iterator getResultIterator(Class resultType)
resultType
parameter. The result
Iterator is valid only for the current transaction.
Use this method when it is desirable to use the EntityManager APIs to access the
resulting entities.
Example:
The following query returns all of the employees and the department they belong to
for one division, ordering by salary. We want to print out the 5 employees with the
highest salaries and then select work with employees from only one department in the
same working set:
em.getTransaction().begin(); String ql = SELECT e.name, e.id, e.dept from Employee e WHERE e.dept.division='Manufacturing' ORDER BY e.salary DESC Query q = em.createQuery(ql); q.setResultEntityName("AllEmployees"); Iterator results = q.getResultIterator(EmployeeResult.class); int curEmployee=0; while(results.hasNext() && curEmployee++ < 5) { EmployeeResult curEmp = (EmployeeResult) results.next(); System.out.println(curEmp); // Remove the employee from the resultset. em.remove(curEmp); } // Flush the changes to the result map. em.flush(); // Run a query against the local working set without the employees we removed String ql = SELECT e.name, e.id, e.dept from AllEmployees e WHERE e.dept.name="Hardware"" Query q = em.createQuery(ql); Iterator results = q.getResultIterator(EmployeeResult.class); while(results.hasNext()) { EmployeeResult curEmp = (EmployeeResult) results.next(); System.out.println(curEmp); }@Entity public class EmployeeResult { String name; Integer id;@ManyToOne Dept dept; public String toString() { return "Name=" + name + ", ID=" + id + ", Department=" + dept.name(); } }
resultType
- the type of object to convert the results into.
PersistenceException
- thrown if query validation fails.
This exception may be deferred until the query is run.
TransactionRequiredException
- if invoked without an active transaction.Object getSingleResult()
If the SELECT clause has more than one field defined, then the result
will be a Object[]
where each element in the object array is based on it's ordinal
position within the query's select clause.
String ql = SELECT e from Employee e WHERE e.id=100" Employee e = em.createQuery(ql).getSingleResult(); String ql = SELECT e.name, e.dept from Employee e WHERE e.id=100" Object[] empData = em.createQuery(ql).getSingleResult(); String empName= (String) empData[0]; Department empDept = (Department) empData[1];
NoResultException
- if there is no result
NonUniqueResultException
- if more than one result
PersistenceException
- thrown if query validation failsQuery setMaxResults(int maxResult)
maxResult
- the maximum number of results to retrieve.
IllegalArgumentException
- if argument is negativeQuery setFirstResult(int startPosition)
startPosition
- position of the first result, numbered from 0
IllegalArgumentException
- if argument is negativeQuery setResultEntityName(String entityName)
Each time the getResultIterator or getResultMap methods are invoked, an entity with an ObjectMap are dynamically created to hold the results of the query. If not specified, or null, the entity and ObjectMap name will be automatically generated.
Since all query results are available for the duration of a transaction, a query name may not be reused in a single transaction.
entityName
- the name of the entity
Query setHint(String hintName, Object value)
hintName
- the name of the hint. See the constant values defined in this interface.value
- the value of the hint.
IllegalArgumentException
- thrown if the second argument is not valid for the hint.
This exception may be deferred until the query is run.Query setParameter(String name, Object value)
name
- the parameter namevalue
- the value of the parameter.
IllegalArgumentException
- if parameter name does not correspond to parameter in query string or argument is of incorrect type.
This exception may be deferred until the query is run.Query setParameter(int position, Object value)
position
- the position of the parameter. The first parameter is 1.value
- the value of the parameter.
IllegalArgumentException
- if position does not correspond to positional parameter of query or argument is of incorrect typel
This exception may be deferred until the query is run.Query setFlushMode(FlushModeType flushMode)
flushMode
- the FlushModeType to set for this query instance.
Query setPartition(int partitionId)
Required if the maps in the query are partitioned and the EntityManager does not have affinity to a single schema root entity's partition.
Use the PartitionManager
to determine the number of partitions
for a given entity's backing map.
partitionId
- the partition to route the query to.
Query setForUpdate(boolean forUpdate)
If not set, the default is false.
forUpdate
- if true, lock records for update.
String getPlan()
This method can be used to explain the plan of a query without running the query. The plan describes when an entity is to be scanned, an index used and the order in which the entities are accessed.
PersistenceException
- thrown if query validation fails. The transaction will not be marked to rollback.
|
IBM WebSphereTM eXtreme Scale, Release 8.6 API Specification |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |