org.apache.openjpa.persistence
Class QueryImpl<X>

java.lang.Object
  extended by org.apache.openjpa.persistence.QueryImpl<X>
All Implemented Interfaces:
Serializable, Query, TypedQuery<X>, OpenJPAQuery<X>, OpenJPAQuerySPI<X>

public class QueryImpl<X>
extends Object
implements OpenJPAQuerySPI<X>, Serializable

Implementation of Query interface.

Author:
Marc Prud'hommeaux, Abe White
See Also:
Serialized Form

Field Summary
 
Fields inherited from interface org.apache.openjpa.persistence.OpenJPAQuery
FLUSH_FALSE, FLUSH_TRUE, FLUSH_WITH_CONNECTION, HINT_RESULT_COUNT, OP_DELETE, OP_SELECT, OP_UPDATE
 
Constructor Summary
QueryImpl(EntityManagerImpl em, Query query)
          Deprecated.  
QueryImpl(EntityManagerImpl em, RuntimeExceptionTranslator ret, Query query)
          Constructor; supply factory exception translator and delegate.
 
Method Summary
 OpenJPAQuery<X> addAggregateListener(AggregateListener listener)
          Register an aggregate listener for the query.
 OpenJPAQuery<X> addFilterListener(FilterListener listener)
          Register a filter listener for the query.
 OpenJPAQuery<X> closeAll()
          Close all open query results.
 OpenJPAQuery<X> compile()
          Compile the query.
 void declareParameter(Object key, Parameter<?> param)
          Declares the given parameter for this query.
 boolean equals(Object other)
           
 int executeUpdate()
           
 Set<?> getBoundParameterKeys()
          Gets the parameter keys bound with this query.
 Collection<?> getCandidateCollection()
          Return the candidate collection, or null if an extent was specified instead of a collection.
 String[] getDataStoreActions(Map params)
          Returns a description of the commands that will be sent to the datastore in order to execute this query.
 Set<?> getDeclaredParameterKeys()
          Gets the declared parameter keys in the given query.
<T> Set<T>
getDeclaredParameterKeys(Class<T> keyType)
           
 Map<Object,Parameter<?>> getDeclaredParameters()
          Gets the parameter instances declared in this query.
 Query getDelegate()
          Delegate.
 OpenJPAEntityManager getEntityManager()
          The owning entity manager.
 FetchPlan getFetchPlan()
          Fetch plan for controlling the loading of results.
 int getFirstResult()
          Return the 0-based start index for the returned results.
 FlushModeType getFlushMode()
          Return the current flush mode.
 Map<String,Object> getHints()
          Get all the active hints and their values.
 boolean getIgnoreChanges()
          Whether to ignore changes in the current transaction.
 String getLanguage()
          Query language.
 LockModeType getLockMode()
           
 int getMaxResults()
          Return the maximum number of results to retrieve.
 Map<String,Object> getNamedParameters()
          Gets the value of all the named parameters.
 QueryOperationType getOperation()
          Query operation type.
 Parameter<?> getParameter(int pos)
          Get the positional parameter with the given position.
<T> Parameter<T>
getParameter(int pos, Class<T> type)
          Get the positional parameter with the given position and type.
 Parameter<?> getParameter(String name)
          Get the parameter object corresponding to the declared parameter of the given name.
<T> Parameter<T>
getParameter(String name, Class<T> type)
          Get the parameter of the given name and type.
 Set<Parameter<?>> getParameters()
          Gets the parameters declared in this query.
 Object getParameterValue(int pos)
          Return the value bound to the parameter.
<T> T
getParameterValue(Parameter<T> p)
          Return the value bound to the parameter.
 Object getParameterValue(String name)
          Return the value bound to the parameter.
 Object[] getPositionalParameters()
          Gets the array of positional parameter values.
 String getQueryString()
          Query string.
 Class getResultClass()
          Query result element type.
 List getResultList()
           
 X getSingleResult()
          Execute a query that returns a single result.
 Set<String> getSupportedHints()
          Gets hints supported by this query.
 int hashCode()
           
 boolean hasPositionalParameters()
          Affirms if declared parameters use position identifier.
 boolean hasSubclasses()
          Whether subclasses are included in the query results.
 boolean isBound(Parameter<?> param)
          Affirms if the given parameter is bound to a value for this query.
 boolean isNative()
           
 OpenJPAQuery<X> removeAggregateListener(AggregateListener listener)
          Remove an aggregate listener from the query.
 OpenJPAQuery<X> removeFilterListener(FilterListener listener)
          Remove a filter listener from the query.
 OpenJPAQuery<X> setCandidateCollection(Collection coll)
          Set a collection of candidates.
 OpenJPAQuery<X> setFirstResult(int startPosition)
           
 OpenJPAQuery<X> setFlushMode(FlushModeType flushMode)
           
 OpenJPAQuery<X> setHint(String key, Object value)
           
 OpenJPAQuery<X> setIgnoreChanges(boolean ignore)
          Whether to ignore changes in the current transaction.
 TypedQuery<X> setLockMode(LockModeType lockMode)
          Sets lock mode on the given query.
 OpenJPAQuery<X> setMaxResults(int max)
           
 OpenJPAQuery<X> setParameter(int position, Calendar value, TemporalType t)
          Sets the value of the given positional parameter after conversion of the given value to the given Temporal Type.
 OpenJPAQuery<X> setParameter(int position, Date value, TemporalType type)
          Sets the value of the given named parameter after conversion of the given value to the given Temporal Type.
 OpenJPAQuery<X> setParameter(int pos, Object value)
          Binds the parameter identified by the given position to the given value.
 TypedQuery<X> setParameter(Parameter<Calendar> p, Calendar cal, TemporalType type)
           
 OpenJPAQuery<X> setParameter(Parameter<Date> p, Date date, TemporalType type)
           
<T> OpenJPAQuery<X>
setParameter(Parameter<T> p, T arg1)
           
 OpenJPAQuery<X> setParameter(String name, Calendar value, TemporalType type)
           
 OpenJPAQuery<X> setParameter(String name, Date value, TemporalType type)
           
 OpenJPAQuery<X> setParameter(String name, Object value)
          Sets the parameter of the given name to the given value.
 OpenJPAQuery<X> setParameters(Map params)
          Sets the values of the parameters from the given Map.
 OpenJPAQuery<X> setParameters(Object... params)
          Binds the given values as positional parameters.
 OpenJPAQuery<X> setResultClass(Class cls)
          Query result element type.
 OpenJPAQuery<X> setSubclasses(boolean subs)
          Whether subclasses are included in the query results.
 String toString()
           
<T> T
unwrap(Class<T> cls)
          Unwraps this receiver to an instance of the given class, if possible.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QueryImpl

public QueryImpl(EntityManagerImpl em,
                 RuntimeExceptionTranslator ret,
                 Query query)
Constructor; supply factory exception translator and delegate.

Parameters:
em - The EntityManager which created this query
ret - Exception translator for this query
query - The underlying "kernel" query.

QueryImpl

public QueryImpl(EntityManagerImpl em,
                 Query query)
Deprecated. 

Constructor; supply factory and delegate.

Method Detail

getDelegate

public Query getDelegate()
Delegate.


getEntityManager

public OpenJPAEntityManager getEntityManager()
Description copied from interface: OpenJPAQuery
The owning entity manager.

Specified by:
getEntityManager in interface OpenJPAQuery<X>

getLanguage

public String getLanguage()
Description copied from interface: OpenJPAQuery
Query language.

Specified by:
getLanguage in interface OpenJPAQuery<X>

getOperation

public QueryOperationType getOperation()
Description copied from interface: OpenJPAQuery
Query operation type.

Specified by:
getOperation in interface OpenJPAQuery<X>

getFetchPlan

public FetchPlan getFetchPlan()
Description copied from interface: OpenJPAQuery
Fetch plan for controlling the loading of results.

Specified by:
getFetchPlan in interface OpenJPAQuery<X>

getQueryString

public String getQueryString()
Description copied from interface: OpenJPAQuery
Query string.

Specified by:
getQueryString in interface OpenJPAQuery<X>

getIgnoreChanges

public boolean getIgnoreChanges()
Description copied from interface: OpenJPAQuery
Whether to ignore changes in the current transaction.

Specified by:
getIgnoreChanges in interface OpenJPAQuery<X>

setIgnoreChanges

public OpenJPAQuery<X> setIgnoreChanges(boolean ignore)
Description copied from interface: OpenJPAQuery
Whether to ignore changes in the current transaction.

Specified by:
setIgnoreChanges in interface OpenJPAQuery<X>

addFilterListener

public OpenJPAQuery<X> addFilterListener(FilterListener listener)
Description copied from interface: OpenJPAQuerySPI
Register a filter listener for the query.

Specified by:
addFilterListener in interface OpenJPAQuery<X>
Specified by:
addFilterListener in interface OpenJPAQuerySPI<X>

removeFilterListener

public OpenJPAQuery<X> removeFilterListener(FilterListener listener)
Description copied from interface: OpenJPAQuerySPI
Remove a filter listener from the query.

Specified by:
removeFilterListener in interface OpenJPAQuery<X>
Specified by:
removeFilterListener in interface OpenJPAQuerySPI<X>

addAggregateListener

public OpenJPAQuery<X> addAggregateListener(AggregateListener listener)
Description copied from interface: OpenJPAQuerySPI
Register an aggregate listener for the query.

Specified by:
addAggregateListener in interface OpenJPAQuery<X>
Specified by:
addAggregateListener in interface OpenJPAQuerySPI<X>

removeAggregateListener

public OpenJPAQuery<X> removeAggregateListener(AggregateListener listener)
Description copied from interface: OpenJPAQuerySPI
Remove an aggregate listener from the query.

Specified by:
removeAggregateListener in interface OpenJPAQuery<X>
Specified by:
removeAggregateListener in interface OpenJPAQuerySPI<X>

getCandidateCollection

public Collection<?> getCandidateCollection()
Description copied from interface: OpenJPAQuery
Return the candidate collection, or null if an extent was specified instead of a collection.

Specified by:
getCandidateCollection in interface OpenJPAQuery<X>

setCandidateCollection

public OpenJPAQuery<X> setCandidateCollection(Collection coll)
Description copied from interface: OpenJPAQuery
Set a collection of candidates.

Specified by:
setCandidateCollection in interface OpenJPAQuery<X>

getResultClass

public Class getResultClass()
Description copied from interface: OpenJPAQuery
Query result element type.

Specified by:
getResultClass in interface OpenJPAQuery<X>

setResultClass

public OpenJPAQuery<X> setResultClass(Class cls)
Description copied from interface: OpenJPAQuery
Query result element type.

Specified by:
setResultClass in interface OpenJPAQuery<X>

hasSubclasses

public boolean hasSubclasses()
Description copied from interface: OpenJPAQuery
Whether subclasses are included in the query results.

Specified by:
hasSubclasses in interface OpenJPAQuery<X>

setSubclasses

public OpenJPAQuery<X> setSubclasses(boolean subs)
Description copied from interface: OpenJPAQuery
Whether subclasses are included in the query results.

Specified by:
setSubclasses in interface OpenJPAQuery<X>

getFirstResult

public int getFirstResult()
Description copied from interface: OpenJPAQuery
Return the 0-based start index for the returned results.

Specified by:
getFirstResult in interface Query
Specified by:
getFirstResult in interface OpenJPAQuery<X>

setFirstResult

public OpenJPAQuery<X> setFirstResult(int startPosition)
Specified by:
setFirstResult in interface Query
Specified by:
setFirstResult in interface TypedQuery<X>
Specified by:
setFirstResult in interface OpenJPAQuery<X>

getMaxResults

public int getMaxResults()
Description copied from interface: OpenJPAQuery
Return the maximum number of results to retrieve. or Integer.MAX_VALUE for no limit.

Specified by:
getMaxResults in interface Query
Specified by:
getMaxResults in interface OpenJPAQuery<X>

setMaxResults

public OpenJPAQuery<X> setMaxResults(int max)
Specified by:
setMaxResults in interface Query
Specified by:
setMaxResults in interface TypedQuery<X>
Specified by:
setMaxResults in interface OpenJPAQuery<X>

compile

public OpenJPAQuery<X> compile()
Description copied from interface: OpenJPAQuery
Compile the query.

Specified by:
compile in interface OpenJPAQuery<X>

getResultList

public List getResultList()
Specified by:
getResultList in interface Query
Specified by:
getResultList in interface TypedQuery<X>

getSingleResult

public X getSingleResult()
Execute a query that returns a single result.

Specified by:
getSingleResult in interface Query
Specified by:
getSingleResult in interface TypedQuery<X>

executeUpdate

public int executeUpdate()
Specified by:
executeUpdate in interface Query

getFlushMode

public FlushModeType getFlushMode()
Description copied from interface: OpenJPAQuery
Return the current flush mode.

Specified by:
getFlushMode in interface Query
Specified by:
getFlushMode in interface OpenJPAQuery<X>

setFlushMode

public OpenJPAQuery<X> setFlushMode(FlushModeType flushMode)
Specified by:
setFlushMode in interface Query
Specified by:
setFlushMode in interface TypedQuery<X>
Specified by:
setFlushMode in interface OpenJPAQuery<X>

isNative

public boolean isNative()

closeAll

public OpenJPAQuery<X> closeAll()
Description copied from interface: OpenJPAQuery
Close all open query results.

Specified by:
closeAll in interface OpenJPAQuery<X>

getDataStoreActions

public String[] getDataStoreActions(Map params)
Description copied from interface: OpenJPAQuery
Returns a description of the commands that will be sent to the datastore in order to execute this query. This will typically be in the native query language of the database (e.g., SQL).

Specified by:
getDataStoreActions in interface OpenJPAQuery<X>
Parameters:
params - the named parameter map for the query invocation

getLockMode

public LockModeType getLockMode()
Specified by:
getLockMode in interface Query

setLockMode

public TypedQuery<X> setLockMode(LockModeType lockMode)
Sets lock mode on the given query. If the target query has been prepared and cached, then ignores the cached version.

Specified by:
setLockMode in interface Query
Specified by:
setLockMode in interface TypedQuery<X>
See Also:
ignorePreparedQuery()

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object other)
Overrides:
equals in class Object

getHints

public Map<String,Object> getHints()
Get all the active hints and their values.

Specified by:
getHints in interface Query

setHint

public OpenJPAQuery<X> setHint(String key,
                               Object value)
Specified by:
setHint in interface Query
Specified by:
setHint in interface TypedQuery<X>
Specified by:
setHint in interface OpenJPAQuery<X>

getSupportedHints

public Set<String> getSupportedHints()
Description copied from interface: OpenJPAQuery
Gets hints supported by this query.

Specified by:
getSupportedHints in interface OpenJPAQuery<X>

unwrap

public <T> T unwrap(Class<T> cls)
Unwraps this receiver to an instance of the given class, if possible.

Specified by:
unwrap in interface Query
Throws:
if - the given class is null, generic Object.class or a class that is not wrapped by this receiver.
Since:
2.0.0

setParameter

public OpenJPAQuery<X> setParameter(int pos,
                                    Object value)
Binds the parameter identified by the given position to the given value. The parameter are bound to a value in the context of this query. The same parameter may be bound to a different value in the context of another query.
For non-native queries, the given position must be a valid position in the declared parameters.
As native queries may not be parsed and hence their declared parameters may not be known, setting an positional parameter has the side-effect of a positional parameter being declared.

Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>
Specified by:
setParameter in interface OpenJPAQuery<X>
Parameters:
position - positive, integer position of the parameter
value - an assignment compatible value
Returns:
the same query instance
Throws:
IllegalArgumentException - if position does not correspond to a positional parameter of the query or if the argument is of incorrect type

setParameter

public OpenJPAQuery<X> setParameter(int position,
                                    Calendar value,
                                    TemporalType t)
Sets the value of the given positional parameter after conversion of the given value to the given Temporal Type.

Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>
Specified by:
setParameter in interface OpenJPAQuery<X>

setParameter

public OpenJPAQuery<X> setParameter(int position,
                                    Date value,
                                    TemporalType type)
Sets the value of the given named parameter after conversion of the given value to the given Temporal Type.

Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>
Specified by:
setParameter in interface OpenJPAQuery<X>

hasPositionalParameters

public boolean hasPositionalParameters()
Affirms if declared parameters use position identifier.

Specified by:
hasPositionalParameters in interface OpenJPAQuery<X>

getPositionalParameters

public Object[] getPositionalParameters()
Gets the array of positional parameter values. The n-th array element represents (n+1)-th positional parameter. If a parameter has been declared but not bound to a value then the value is null and hence is indistinguishable from the value being actually null. If the parameter indexing is not contiguous then the unspecified parameters are considered as null.

Specified by:
getPositionalParameters in interface OpenJPAQuery<X>

setParameters

public OpenJPAQuery<X> setParameters(Object... params)
Binds the given values as positional parameters. The n-th array element value is set to a Parameter with (n+1)-th positional identifier.

Specified by:
setParameters in interface OpenJPAQuery<X>

getNamedParameters

public Map<String,Object> getNamedParameters()
Gets the value of all the named parameters. If a parameter has been declared but not bound to a value then the value is null and hence is indistinguishable from the value being actually null.

Specified by:
getNamedParameters in interface OpenJPAQuery<X>

setParameters

public OpenJPAQuery<X> setParameters(Map params)
Sets the values of the parameters from the given Map. The keys of the given map designate the name of the declared parameter.

Specified by:
setParameters in interface OpenJPAQuery<X>

getParameter

public <T> Parameter<T> getParameter(String name,
                                     Class<T> type)
Get the parameter of the given name and type.

Specified by:
getParameter in interface Query
Throws:
IllegalArgumentException - if the parameter of the specified name does not exist or is not assignable to the type
IllegalStateException - if invoked on a native query

getParameter

public <T> Parameter<T> getParameter(int pos,
                                     Class<T> type)
Get the positional parameter with the given position and type.

Specified by:
getParameter in interface Query
Throws:
IllegalArgumentException - if the parameter with the specified position does not exist or is not assignable to the type
IllegalStateException - if invoked on a native query unless the same parameter position is bound already.

getParameterValue

public <T> T getParameterValue(Parameter<T> p)
Return the value bound to the parameter.

Specified by:
getParameterValue in interface Query
Parameters:
param - parameter object
Returns:
parameter value
Throws:
IllegalStateException - if the parameter has not been been bound
IllegalArgumentException - if the parameter does not belong to this query

getParameters

public Set<Parameter<?>> getParameters()
Gets the parameters declared in this query.

Specified by:
getParameters in interface Query

setParameter

public <T> OpenJPAQuery<X> setParameter(Parameter<T> p,
                                        T arg1)
Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>

setParameter

public OpenJPAQuery<X> setParameter(Parameter<Date> p,
                                    Date date,
                                    TemporalType type)
Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>

setParameter

public TypedQuery<X> setParameter(Parameter<Calendar> p,
                                  Calendar cal,
                                  TemporalType type)
Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>

getParameter

public Parameter<?> getParameter(String name)
Get the parameter object corresponding to the declared parameter of the given name. This method is not required to be supported for native queries.

Specified by:
getParameter in interface Query
Throws:
IllegalArgumentException - if the parameter of the specified name does not exist
IllegalStateException - if invoked on a native query

getParameter

public Parameter<?> getParameter(int pos)
Get the positional parameter with the given position. The parameter may just have been declared and not bound to a value.

Specified by:
getParameter in interface Query
Parameters:
position - specified in the user query.
Returns:
parameter object
Throws:
IllegalArgumentException - if the parameter with the given position does not exist

getParameterValue

public Object getParameterValue(String name)
Return the value bound to the parameter.

Specified by:
getParameterValue in interface Query
Parameters:
name - name of the parameter
Returns:
parameter value
Throws:
IllegalStateException - if this parameter has not been bound

getParameterValue

public Object getParameterValue(int pos)
Return the value bound to the parameter.

Specified by:
getParameterValue in interface Query
Parameters:
pos - position of the parameter
Returns:
parameter value
Throws:
IllegalStateException - if this parameter has not been bound

getBoundParameterKeys

public Set<?> getBoundParameterKeys()
Gets the parameter keys bound with this query. Parameter key can be Integer, String or a ParameterExpression itself but all parameters keys of a particular query are of the same type.


getDeclaredParameterKeys

public Set<?> getDeclaredParameterKeys()
Gets the declared parameter keys in the given query. This information is only available after the query has been parsed. As native language queries are not parsed, this information is not available for them.

Returns:
set of parameter identifiers in a parsed query

getDeclaredParameterKeys

public <T> Set<T> getDeclaredParameterKeys(Class<T> keyType)

getDeclaredParameters

public Map<Object,Parameter<?>> getDeclaredParameters()
Gets the parameter instances declared in this query. All parameter keys are of the same type. It is not allowed to mix keys of different type such as named and positional keys. For string-based queries, the parser supplies the information about the declared parameters as a LinkedMap of expected parameter value type indexed by parameter identifier. For non string-based queries that a facade itself may construct (e.g. CriteriaQuery), the parameters must be declared by the caller. This receiver constructs concrete Parameter instances from the given parameter identifiers.

Returns:
a Map where the key represents the original identifier of the parameter (can be a String, Integer or a ParameterExpression itself) and the value is the concrete Parameter instance either constructed as a result of this call or supplied by declaring the parameter explicitly via #declareParameter(Parameter).

declareParameter

public void declareParameter(Object key,
                             Parameter<?> param)
Declares the given parameter for this query. Used by non-string based queries that are constructed by the facade itself rather than OpenJPA parsing the query to detect the declared parameters.

Parameters:
key - this is the key to identify the parameter later in the context of this query. Valid key types are Integer, String or ParameterExpression itself.
the - parameter instance to be declared

isBound

public boolean isBound(Parameter<?> param)
Affirms if the given parameter is bound to a value for this query.

Specified by:
isBound in interface Query

setParameter

public OpenJPAQuery<X> setParameter(String name,
                                    Calendar value,
                                    TemporalType type)
Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>
Specified by:
setParameter in interface OpenJPAQuery<X>

setParameter

public OpenJPAQuery<X> setParameter(String name,
                                    Date value,
                                    TemporalType type)
Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>
Specified by:
setParameter in interface OpenJPAQuery<X>

setParameter

public OpenJPAQuery<X> setParameter(String name,
                                    Object value)
Sets the parameter of the given name to the given value.

Specified by:
setParameter in interface Query
Specified by:
setParameter in interface TypedQuery<X>
Specified by:
setParameter in interface OpenJPAQuery<X>

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2006-2010 Apache Software Foundation. All Rights Reserved.