Package org.apache.openjpa.persistence
Class AbstractQuery<X>
- java.lang.Object
-
- org.apache.openjpa.persistence.AbstractQuery<X>
-
- All Implemented Interfaces:
jakarta.persistence.Query,jakarta.persistence.TypedQuery<X>,OpenJPAQuery<X>,OpenJPAQuerySPI<X>
- Direct Known Subclasses:
QueryImpl
public abstract class AbstractQuery<X> extends java.lang.Object implements OpenJPAQuerySPI<X>
An abstract implementation of the Query interface.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<jakarta.persistence.Parameter<?>,java.lang.Object>_boundParamsprotected boolean_convertPositionalParamsprotected java.util.Map<java.lang.Object,jakarta.persistence.Parameter<?>>_declaredParamsprotected EntityManagerImpl_emprotected QueryMetaData_qmdprotected boolean_relaxBindParameterTypeChecking-
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
Constructors Constructor Description AbstractQuery(QueryMetaData qmd, EntityManagerImpl em)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidassertOpen()voiddeclareParameter(java.lang.Object key, jakarta.persistence.Parameter<?> param)Declares the given parameter for this query.java.util.Set<?>getBoundParameterKeys()Gets the parameter keys bound with this query.java.util.Set<?>getDeclaredParameterKeys()Gets the declared parameter keys in the given query.<T> java.util.Set<T>getDeclaredParameterKeys(java.lang.Class<T> keyType)java.util.Map<java.lang.Object,jakarta.persistence.Parameter<?>>getDeclaredParameters()Gets the parameter instances declared in this query.java.util.Map<java.lang.String,java.lang.Object>getNamedParameters()Gets the value of all the named parameters.jakarta.persistence.Parameter<?>getParameter(int pos)Get the positional parameter with the given position.<T> jakarta.persistence.Parameter<T>getParameter(int pos, java.lang.Class<T> type)Get the positional parameter with the given position and type.jakarta.persistence.Parameter<?>getParameter(java.lang.String name)Get the parameter object corresponding to the declared parameter of the given name.<T> jakarta.persistence.Parameter<T>getParameter(java.lang.String name, java.lang.Class<T> type)Get the parameter of the given name and type.java.util.Set<jakarta.persistence.Parameter<?>>getParameters()Gets the parameters declared in this query.java.lang.ObjectgetParameterValue(int pos)Return the value bound to the parameter.<T> TgetParameterValue(jakarta.persistence.Parameter<T> p)Return the value bound to the parameter.java.lang.ObjectgetParameterValue(java.lang.String name)Return the value bound to the parameter.protected abstract OrderedMap<java.lang.Object,java.lang.Class<?>>getParamTypes()java.lang.Object[]getPositionalParameters()Gets the array of positional parameter values.booleangetRelaxBindParameterTypeChecking()Gets whether the type of user-supplied bind parameter value and the type of target persistent property they bind to are checked with strong or weak constraint.booleanhasPositionalParameters()Affirms if declared parameters use position identifier.booleanisBound(jakarta.persistence.Parameter<?> param)Affirms if the given parameter is bound to a value for this query.booleanisNative()booleanisProcedure()protected abstract voidlock()OpenJPAQuery<X>setParameter(int pos, java.lang.Object value)Binds the parameter identified by the given position to the given value.OpenJPAQuery<X>setParameter(int position, java.util.Calendar value, jakarta.persistence.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, java.util.Date value, jakarta.persistence.TemporalType type)Sets the value of the given named parameter after conversion of the given value to the given Temporal Type.jakarta.persistence.TypedQuery<X>setParameter(jakarta.persistence.Parameter<java.util.Calendar> p, java.util.Calendar cal, jakarta.persistence.TemporalType type)OpenJPAQuery<X>setParameter(jakarta.persistence.Parameter<java.util.Date> p, java.util.Date date, jakarta.persistence.TemporalType type)<T> OpenJPAQuery<X>setParameter(jakarta.persistence.Parameter<T> p, T arg1)OpenJPAQuery<X>setParameter(java.lang.String name, java.lang.Object value)Sets the parameter of the given name to the given value.OpenJPAQuery<X>setParameter(java.lang.String name, java.util.Calendar value, jakarta.persistence.TemporalType type)OpenJPAQuery<X>setParameter(java.lang.String name, java.util.Date value, jakarta.persistence.TemporalType type)OpenJPAQuery<X>setParameters(java.lang.Object... params)Binds the given values as positional parameters.OpenJPAQuery<X>setParameters(java.util.Map params)Sets the values of the parameters from the given Map.voidsetRelaxBindParameterTypeChecking(java.lang.Object value)Sets whether the type of user-supplied bind parameter value and the type of target persistent property they bind to are checked with strong or weak constraint.protected abstract voidunlock()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.openjpa.persistence.OpenJPAQuery
closeAll, compile, getCandidateCollection, getDataStoreActions, getEntityManager, getFetchPlan, getFirstResult, getFlushMode, getIgnoreChanges, getLanguage, getMaxResults, getOperation, getQueryString, getResultClass, getSupportedHints, hasSubclasses, setCandidateCollection, setFirstResult, setFlushMode, setHint, setIgnoreChanges, setMaxResults, setResultClass, setSubclasses
-
Methods inherited from interface org.apache.openjpa.persistence.OpenJPAQuerySPI
addAggregateListener, addFilterListener, removeAggregateListener, removeFilterListener
-
-
-
-
Field Detail
-
_relaxBindParameterTypeChecking
protected boolean _relaxBindParameterTypeChecking
-
_convertPositionalParams
protected boolean _convertPositionalParams
-
_qmd
protected final QueryMetaData _qmd
-
_em
protected transient EntityManagerImpl _em
-
_boundParams
protected java.util.Map<jakarta.persistence.Parameter<?>,java.lang.Object> _boundParams
-
_declaredParams
protected java.util.Map<java.lang.Object,jakarta.persistence.Parameter<?>> _declaredParams
-
-
Constructor Detail
-
AbstractQuery
public AbstractQuery(QueryMetaData qmd, EntityManagerImpl em)
-
-
Method Detail
-
isProcedure
public boolean isProcedure()
-
isNative
public boolean isNative()
-
assertOpen
protected abstract void assertOpen()
-
lock
protected abstract void lock()
-
unlock
protected abstract void unlock()
-
getParamTypes
protected abstract OrderedMap<java.lang.Object,java.lang.Class<?>> getParamTypes()
- Returns:
- a map of parameter name to type for this query.
-
setParameter
public OpenJPAQuery<X> setParameter(int pos, java.lang.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:
setParameterin interfaceOpenJPAQuery<X>- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>- Parameters:
position- positive, integer position of the parametervalue- an assignment compatible value- Returns:
- the same query instance
- Throws:
java.lang.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, java.util.Calendar value, jakarta.persistence.TemporalType t)
Sets the value of the given positional parameter after conversion of the given value to the given Temporal Type.- Specified by:
setParameterin interfaceOpenJPAQuery<X>- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
setParameter
public OpenJPAQuery<X> setParameter(int position, java.util.Date value, jakarta.persistence.TemporalType type)
Sets the value of the given named parameter after conversion of the given value to the given Temporal Type.- Specified by:
setParameterin interfaceOpenJPAQuery<X>- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
hasPositionalParameters
public boolean hasPositionalParameters()
Affirms if declared parameters use position identifier.- Specified by:
hasPositionalParametersin interfaceOpenJPAQuery<X>
-
getPositionalParameters
public java.lang.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:
getPositionalParametersin interfaceOpenJPAQuery<X>
-
setParameters
public OpenJPAQuery<X> setParameters(java.lang.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:
setParametersin interfaceOpenJPAQuery<X>
-
getNamedParameters
public java.util.Map<java.lang.String,java.lang.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:
getNamedParametersin interfaceOpenJPAQuery<X>
-
setParameters
public OpenJPAQuery<X> setParameters(java.util.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:
setParametersin interfaceOpenJPAQuery<X>
-
getParameter
public <T> jakarta.persistence.Parameter<T> getParameter(java.lang.String name, java.lang.Class<T> type)Get the parameter of the given name and type.- Specified by:
getParameterin interfacejakarta.persistence.Query- Throws:
java.lang.IllegalArgumentException- if the parameter of the specified name does not exist or is not assignable to the typejava.lang.IllegalStateException- if invoked on a native query
-
getParameter
public <T> jakarta.persistence.Parameter<T> getParameter(int pos, java.lang.Class<T> type)Get the positional parameter with the given position and type.- Specified by:
getParameterin interfacejakarta.persistence.Query- Throws:
java.lang.IllegalArgumentException- if the parameter with the specified position does not exist or is not assignable to the typejava.lang.IllegalStateException- if invoked on a native query unless the same parameter position is bound already.
-
getParameterValue
public <T> T getParameterValue(jakarta.persistence.Parameter<T> p)
Return the value bound to the parameter.- Specified by:
getParameterValuein interfacejakarta.persistence.Query- Parameters:
param- parameter object- Returns:
- parameter value
- Throws:
java.lang.IllegalStateException- if the parameter has not been been boundjava.lang.IllegalArgumentException- if the parameter does not belong to this query
-
getParameters
public java.util.Set<jakarta.persistence.Parameter<?>> getParameters()
Gets the parameters declared in this query.- Specified by:
getParametersin interfacejakarta.persistence.Query
-
setParameter
public <T> OpenJPAQuery<X> setParameter(jakarta.persistence.Parameter<T> p, T arg1)
- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
setParameter
public OpenJPAQuery<X> setParameter(jakarta.persistence.Parameter<java.util.Date> p, java.util.Date date, jakarta.persistence.TemporalType type)
- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
setParameter
public jakarta.persistence.TypedQuery<X> setParameter(jakarta.persistence.Parameter<java.util.Calendar> p, java.util.Calendar cal, jakarta.persistence.TemporalType type)
- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
getParameter
public jakarta.persistence.Parameter<?> getParameter(java.lang.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:
getParameterin interfacejakarta.persistence.Query- Throws:
java.lang.IllegalArgumentException- if the parameter of the specified name does not existjava.lang.IllegalStateException- if invoked on a native query
-
getParameter
public jakarta.persistence.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:
getParameterin interfacejakarta.persistence.Query- Parameters:
position- specified in the user query.- Returns:
- parameter object
- Throws:
java.lang.IllegalArgumentException- if the parameter with the given position does not exist
-
getParameterValue
public java.lang.Object getParameterValue(java.lang.String name)
Return the value bound to the parameter.- Specified by:
getParameterValuein interfacejakarta.persistence.Query- Parameters:
name- name of the parameter- Returns:
- parameter value
- Throws:
java.lang.IllegalStateException- if this parameter has not been bound
-
getParameterValue
public java.lang.Object getParameterValue(int pos)
Return the value bound to the parameter.- Specified by:
getParameterValuein interfacejakarta.persistence.Query- Parameters:
pos- position of the parameter- Returns:
- parameter value
- Throws:
java.lang.IllegalStateException- if this parameter has not been bound
-
getBoundParameterKeys
public java.util.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 java.util.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> java.util.Set<T> getDeclaredParameterKeys(java.lang.Class<T> keyType)
-
getDeclaredParameters
public java.util.Map<java.lang.Object,jakarta.persistence.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(java.lang.Object key, jakarta.persistence.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(jakarta.persistence.Parameter<?> param)
Affirms if the given parameter is bound to a value for this query.- Specified by:
isBoundin interfacejakarta.persistence.Query
-
setParameter
public OpenJPAQuery<X> setParameter(java.lang.String name, java.util.Calendar value, jakarta.persistence.TemporalType type)
- Specified by:
setParameterin interfaceOpenJPAQuery<X>- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
setParameter
public OpenJPAQuery<X> setParameter(java.lang.String name, java.util.Date value, jakarta.persistence.TemporalType type)
- Specified by:
setParameterin interfaceOpenJPAQuery<X>- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
setParameter
public OpenJPAQuery<X> setParameter(java.lang.String name, java.lang.Object value)
Sets the parameter of the given name to the given value.- Specified by:
setParameterin interfaceOpenJPAQuery<X>- Specified by:
setParameterin interfacejakarta.persistence.Query- Specified by:
setParameterin interfacejakarta.persistence.TypedQuery<X>
-
getRelaxBindParameterTypeChecking
public boolean getRelaxBindParameterTypeChecking()
Description copied from interface:OpenJPAQueryGets whether the type of user-supplied bind parameter value and the type of target persistent property they bind to are checked with strong or weak constraint.- Specified by:
getRelaxBindParameterTypeCheckingin interfaceOpenJPAQuery<X>- Returns:
- the booelan state. False by default, i.e. the type of a bind parameter value is checked strongly against the target property type.
-
setRelaxBindParameterTypeChecking
public void setRelaxBindParameterTypeChecking(java.lang.Object value)
Description copied from interface:OpenJPAQuerySets whether the type of user-supplied bind parameter value and the type of target persistent property they bind to are checked with strong or weak constraint.
The same can be set viahintwithout puncturing standard JPA API.- Specified by:
setRelaxBindParameterTypeCheckingin interfaceOpenJPAQuery<X>- Parameters:
value- a String or Boolean value.- See Also:
Filters.canConvert(Class, Class, boolean),Filters.convert(Object, Class, boolean)
-
-